[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
Keith Dear <[email protected]> |

To |
[email protected] |

Subject |
Re: st: Obtaining varlist from other dataset |

Date |
Sat, 02 Feb 2008 16:57:16 +1100 |

David,

Wilner is right, the varlist option does solve your problem. But the statement:

. local myvars r(varlist)

merely assigns the string "r(varlist)" to the macro, which therefore contains only one word.

You can verify this with

. display "`myvars'"

r(varlist)

You can get what you need like this:

. describe using my_file, varlist

. local myvars=r(varlist) // "=" inserted

. display "`myvars'"

x1 x2 x3 x4

The "=" has the effect of *evaluating* r(varlist), and assigning the result to myvars.

Keith

At 03:42 PM 2/02/2008, you wrote:

On Feb 1, 2008 9:15 PM, <[email protected]> wrote:

> Hi.

>

> Is it possible to get a list of the variables from a dataset not currently in memory -- say in a macro?

>

> If I do

> des using otherdataset

> I see the variables in that dataset. But can I get those variables into a macro?

>

> Presently, I -use- the other dataset (-in 1-, to save space, but not time), and then -unab-. It works, but is there a more direct way, without loading the data?

>

> I have a vague sense that I've asked this before; so excuse me if I've forgotten the answer.

>

> Thanks

> --David

David, if you specify the varlist option with describe, Stata will

save the names of the variables in a macro called r(varlist) from

there you can capture them. Apparently, that's all you can get. It

seems that no extended macro functions work on that macro. Though you

may have more than one variable in there, Stata considers them as one

single element. See code below. Another thing, the variables may be

too many to be put in a macro, in which case an error will occur.

clear

input x1 x2 x3 x4

x1 x2 x3 x4

1. 1 1 3 4 5

2. 2 3 4 6 7

3. 4 1 23 45

4. 12 23 1 2

5. end

. save my_file

file my_file.dta saved

. clear

. input z1 z2 z3

z1 z2 z3

1. 20 30 50

2. 12 12 34

3. 12 34 56

4. 12 23 10

5. end

. describe using my_file, varlist

Contains data

obs: 4 1 Feb 2008 23:30

vars: 4

size: 80

-------------------------------------------------------------------------------

storage display value

variable name type format label variable label

-------------------------------------------------------------------------------

x1 float %9.0g

x2 float %9.0g

x3 float %9.0g

x4 float %9.0g

-------------------------------------------------------------------------------

Sorted by:

. local myvars r(varlist)

. local n: word count `myvars' // this returns 1

. di `myvars' "==" `n'

x1 x2 x3 x4==1

.

Likewise, if I try to put them in separate macros, it would be to no avail

. local myvars r(varlist)

. local j=1

. foreach v of local myvars {

2. local vlab: word `j' of `myvars'

3. local var`j' `vlab'

4. di "var `j' is `var`j''"

5. local j=`j'+1

6. }

var 1 is r(varlist)

However, if I do the following, I obtain expected results

. local myvars x1 x2 x3 x4

. local nvar: word count `myvars'

. di `nvar'

4

. local j=1

. foreach v of local myvars {

2. local vlab: word `j' of `myvars'

3. local var`j' `vlab'

4. di "Var `j' is `var`j''"

5. local j=`j'+1

6. }

Var 1 is x1

Var 2 is x2

Var 3 is x3

Var 4 is x4

Others may have other thoughts.

--

P. Wilner Jeanty, Post-doctoral researcher

Dept. of Agricultural, Environmental, and Development Economics

The Ohio State University

2120 Fyffe Road

Columbus, Ohio 43210

(614) 292-6382 (Office)

*

* 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/

* * 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/

**References**:**st: Re: Obtaining varlist from other dataset***From:*"P. Wilner Jeanty" <[email protected]>

- Prev by Date:
**Re: st: R Array [was: Mata for data management]** - Next by Date:
**st: Re:Obtaining varlist from other dataset** - Previous by thread:
**st: Re: Obtaining varlist from other dataset** - Next by thread:
**st: Re:Obtaining varlist from other dataset** - Index(es):

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