Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: save "`table1'"


From   Rongrong Zhang <r05zhang@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: save "`table1'"
Date   Fri, 27 Dec 2013 21:46:08 -0500

Thank you very much , Sarah!  I appreciate you offer such detailed
explanation. You might notice my question extends from my earlier
posting on matching variable across two tables. Robert offered a
program to first create a crosswalk, save it as a temp file `table 1',
then do exact match with my second dataset naics . I ran the following
code (adapted from Robert's program), and got an error when merging

error  "variable naics does not uniquely identify observations in the
master data",

I assume it is `table 1' -master data" with duplicate value? I am not
sure. I copy and paste code and part of `table 1' below .

I need some help regarding:
1. how to identify the problem in `table 1' - e.g. eliminate duplicate
2. if I fix #1 above, would that solve the merging problem.
3. what is crosswalk in stata?

thanks to all who read this posting !!!

****************          code  begins      **************

cd "E:\Data\BEA"

use ionaics3,clear

 * save naics to ionumber crosswalk

isid naics, sort

list, noobs sepby(ionumber)

tempfile table1

 save "`table1'"

***                             Import NAICS ALL DATA             ***

clear

*cd "E:\Data\BEA\NAICSH"

insheet using NAICS_ALL.csv

* (1 var, 1425 obs)


* generate id (goes from 1- n, n is max # of obs

gen table2id = _n

tostring naics, replace


replace naics = regexr(naics,"0+$","")



* do an exact match using the crosswalk

merge 1:1 naics using "`table1'", keepusing(ionumber) ///

keep(master match) nogen



****************          code  ends      **************


****************  `table 1' (can't post the whole thing, statalist
email bounced ****************
  |--------------------------------------------------------------------------------------------------------------------|
  |     1120      1
             Animal production      112    1120 |
  |--------------------------------------------------------------------------------------------------------------------|
  |   112100      1
   Cattle ranching and farming     1121    1121 |
  |--------------------------------------------------------------------------------------------------------------------|
  |   112A00      1                             Animal production,
except cattle and poultry and eggs     1122    112A |
  |--------------------------------------------------------------------------------------------------------------------|
  |   112300      1
    Poultry and egg production     1123    1123 |
  |--------------------------------------------------------------------------------------------------------------------|
  |   112A00      2                             Animal production,
except cattle and poultry and eggs     1124    112A |
  |   112A00      3                             Animal production,
except cattle and poultry and eggs     1125    112A |
  |   112A00      4                             Animal production,
except cattle and poultry and eggs     1129    112A |
  |     1130      1
             Animal production      113    1130 |

  |   113A00      1                              Forest nurseries,
forest products, and timber tracts     1131    113A |
  |   113A00      2                              Forest nurseries,
forest products, and timber tracts     1132    113A |


On Fri, Dec 27, 2013 at 2:58 PM, Sarah Edgington <sedging@ucla.edu> wrote:
> The problem is that you're running line-by-line.
> Stata does not retain local macros (including tempfiles) across sessions.  You'll notice that when you select text in a do file and hit do that Stata issues a -do- command that references a .tmp file. From Stata's perspective each time you hit do on a selected block of code you're running a new file.  Stata will not retain the local macros from the last do file it ran.  So if the local is not defined within the block of code that you're running, the local will be undefined.  If you run all the parts of the dofile that reference `table1' at once you shouldn't have a problem.  Alternatively you can type (or copy) lines into the command line.  As long as you don't close Stata or -do- or -run- a new dofile you'll retain your local macros.
>
> In short, locals are only available in the following contexts:
> 1. within a single interactive sessions
> 2. within a single do file.  Remember that even if it's saved in a single file, if you aren't running the whole thing at once Stata will treat each separate block of commands that you run as a new do file.
> 3. within a single program.  (If you're not using the -program- command don't worry about this).
>
> The documentation for the macro commands is geared toward programming in the sense of using the -program- command.  Nonetheless, if you're using either local or global macros in your do files you should read the section of the manual Phil pointed out to get a better understanding of how they work.
>
> -Sarah
>
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Rongrong Zhang
> Sent: Friday, December 27, 2013 11:13 AM
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: save "`table1'"
>
> Thank you Phil!
>
> Yes , before save "`table1'", there was
> tempfile table1
>
> save "`table1'"
>
> but I ran line by line, why did I get
> ++++++++++++++
>
> . tempfile table1
>
> .
> end of do-file
>
> . do "C:\Users\rzhang\AppData\Local\Temp\STD00000000.tmp"
>
> . save "`table1'"
> invalid file specification
> ++++++++++++++
>
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index