How can I create a varlist?

18,797 views
Skip to first unread message

VM

unread,
Aug 10, 2010, 5:58:22 AM8/10/10
to Stata Users Forum
I assume my question may appear very simple but I am stuck in how to
create a varlist.
I really don't find any usefull guide in the net and the Stata Help
file is very unclear to me.

Can anybody kindly give an example on how to do it?

Thanks!

J

unread,
Aug 11, 2010, 11:01:27 AM8/11/10
to Stata Users Forum
Can you give some context regarding what you are trying to do?

Technically you can't create and stor a varlist. A varlist just a list
of variables separated by white space. Example: age gender income
race .

In a foreach loop you can tell Stata you are providing a varlist

foreach var of varlist age gender income race {

}

Or you can store a varlist in a local or global macro

local myvars age gender income race


I hope this helps.

Jared

unread,
Aug 11, 2010, 11:42:20 AM8/11/10
to stata-us...@googlegroups.com
Variables in a varlist must also already exist in the current working
dataset, otherwise it's just a namelist.

}


I hope this helps.

--
You received this message because you are subscribed to the Google Groups
"Stata Users Forum" group.
To post to this group, send email to stata-us...@googlegroups.com.
To unsubscribe from this group, send email to
stata-users-fo...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/stata-users-forum?hl=en.


Eric A. Booth

unread,
Aug 12, 2010, 10:03:31 AM8/12/10
to Stata Users Forum

see -help macro- and -help ds-

****************************!
sysuse auto, clear

//1. Create using local/global macro

local myvars1 mpg price rep78
di "`myvars1'"
global myvars2 mpg price weight
di "$myvars2"

**you can manipulate these lists:
local oneminustwo: list myvars1 - global(myvars2)
local intersectiononetwo: list myvars1 & global(myvars2)
di "`oneminustwo'"
di "`intersectiononetwo'"

//2. Create and store varlists using -ds-

ds m*
di "`r(varlist)'"
ds, has(type string)
di "`r(varlist)'"
ds, has(type numeric)
di "`r(varlist)'"
ds, has(varlab "*m*")
di "`r(varlist)'"
local myvars3 `r(varlist)'

di "`myvars3'"

**use multiple varlists in a loop**
foreach v in `myvars3' `myvars1' $myvars2 {
di "Summary Table for variable: `v'"
su `v'
}

****************************!
~ Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebo...@ppri.tamu.edu
eric.a...@gmail.com
Office: +979.845.6754

Reply all
Reply to author
Forward
0 new messages