Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Stringing an entire dataset


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Stringing an entire dataset
Date   Thu, 22 Jul 2004 14:55:05 +0100

Roger's code shows too little faith in 
official Stata. 

First, * is fine as a wildcard for 
all variables, so recourse to -unab- 
is unnecessary. 

Second, -tostring- is smart enough 
to pass existing string variables 
without an error. (The psychology 
presumably was that if you want something
to be string, it is not bad news if 
it already is.) 

Thus, 

tostring *, replace 

should suffice. Here I am not applying
-force-: if any variables remain problem 
variables after this procedure, further
thought is needed. Otherwise there is a danger 
of loss of precision through -force-. 

Nick 
[email protected] 

Roger Newson replied to Mart�n Rio wrote:

> >I'm trying to stack several datasets. Each dataset has 
> roughly 50 variables.
> >Some of these are string and some numeric. The source of my 
> problem is that
> >any given variable could be numeric in one dataset and 
> string in another.
> >When these two fields are stacked, Stata ignores the values 
> of this variable
> >in the using dataset.
> >
> >I thought that one way around this problem would be to 
> string all variables
> >in the data, then stack them, and then destring those that I 
> want to be
> >numeric, at the end. Can anyone think of code that would string all
> >variables that are not already string, keeping the same name 
> variable names?
> >I have had no luck so far.
> 
> Probably you should use the -unab- command to get a list of 
> all variables, 
> and -confirm- to check their types. Something like:
> 
> unab allvars: *
> foreach X of var `allvars' {
>    capture confirm numeric var `X'
>    if _rc==0 {
>      tostring `X', force replace
>    }
> }
> 
> I hope this helps.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index