Statalist


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

Re: st: assign values of a variable to a macro


From   "Ben Jann" <[email protected]>
To   [email protected]
Subject   Re: st: assign values of a variable to a macro
Date   Fri, 9 Jan 2009 22:51:13 +0100

Maybe the -valuesof- package is of help. See

 . ssc describe valuesof

ben


On Tue, Jan 6, 2009 at 7:57 PM, Jacob Wegelin <[email protected]> wrote:
> Is there an elegant way to place the contents of a string variable into a
> macro?
>
> The following code places the contents of variables into macros, but seems
> rather clumsy.
>
> Placing the contents of a variable into a macro could be useful. For
> instance, suppose we have a first, small dataset in which one of the
> variables is a list of names which identify a subset of the variables in a
> second dataset.  We might like to keep the second dataset open while
> performing an operation in turn on each of the variables listed in the
> first, small dataset.
>
>
> /* Create a toy dataset */
> clear all
> input X str10 name str10 mixed
> 5 "dog" 1
> 2 "cat" "rat"
> 1 "mouse" 55
> 3 "cat" "eat"
> end
> list
>
> local N= _N
> local Xlist local namelist
> local mixedlist
> forvalues thisrow=1 (1) `N' {
>        local thisX=X[`thisrow']
>        local thisname=name[`thisrow']
>        local thismixed=mixed[`thisrow']
>        local Xlist `Xlist' `thisX'
>        local namelist `namelist' `thisname'
>        local mixedlist `mixedlist' `thismixed'
> }
>        di "`Xlist'"
>        di "`namelist'"
>        di "`mixedlist'"
>
> foreach thisvar in `namelist' {
>        di "This variable is `thisvar'"
>        /* Do something with this variable */
> }
> exit
>
> Surely there's a smarter way to define these macro lists?
>
>
> Jacob A. Wegelin
> Assistant Professor
> Department of Biostatistics
> Virginia Commonwealth University
> 730 East Broad Street Room 3006
> P. O. Box 980032
> Richmond VA 23298-0032
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/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/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