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]

[no subject]



In your example, one variable is problematic. In that case something like

forval i = 1/50 {
    use state`i'
    destring var1, replace
    save new_state`i'
}

should fix the problem.  In particular,

1. Whenever -var1- is numeric, -destring- won't complain.

2. If -destring- finds non-numeric characters in a string variable it
will stop the loop.

In the last case, you will need to reverse the process:

forval i = 1/50 {
     use state`i'
     tostring var1, replace
     save new_state`i'
}

3. If more variables are problematic, you need to add them to the
-destring- or -tostring- statements.

(-tostring- is a command, not a function, in Stata.)

4. I'm recommending new filenames for changed datasets as a matter of
general caution. If you have to iterate, you can always overwrite
new_state1, etc., so long as your originals are safe somewhere.

Nick
[email protected]


On 8 May 2013 01:59, Erika Kociolek <[email protected]> wrote:
> Hi there,
>
> I am working with a large number of datasets that have some variables
> in common. I'll use the two datasets below as an example to illustrate
> my question:
>
> State1.dta
>
> var1 var2
> .         1
> 1        .
> 4        .
> 13     2
>
>
> State2.dta
>
> var1 var3
>           .
> 1'       2
> 3        3
> 4        4
>
> As you can see above, the files State1 and State2 have var1 in common.
> I would like to append the State1 and State2 files together to create
> a master file containing data for both states (some fields will be
> blank because those datapoints may not have been collected for a given
> state).
>
> Due to some issues with the source datasets, in some of the State
> files, var1 is a string (as is the case with State2) and in others,
> var1 is numeric (as is the case with State1). Since the variable types
> are different, I cannot append one file to the other.
>
> My question: is there is any way to define that certain variables  be
> imported as a particular type of variable (such as a string variable),
> instead of having to import files and then generate new string
> variables using a function like "tostring"? I'd like to be able to get
> the variables in all .dta files defined consistently so it is easy to
> append them to a master datafile.
>
> Thanks!
> Erika
> *
> *   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