Statalist The Stata Listserver


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

Re: st: executing a command based on the prefix of variables


From   "Tim Wade" <wadetj@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: executing a command based on the prefix of variables
Date   Fri, 29 Sep 2006 17:44:49 -0400

Yusuke, would this work?

foreach pre in telephone xxx yyy zzz {
tabstat `pre'*, s(mean n max min med) c(s)
}

Tim




On 9/28/06, Yusuke Taishi <yusuke@ifmr.ac.in> wrote:
Thanks for your help and sorry for the ambiguity in my explanation.

I was able to construct a foreach command that generates the same
results as your suggestions.
But the results from tabstat are displayed like the following;

    variable |      mean         N       max       min       p50
-------------+--------------------------------------------------
   telephone |  1.960888      2659         2         1         2
----------------------------------------------------------------

    variable |      mean         N       max       min       p50
-------------+--------------------------------------------------
 telephoneno |      1.25       104         5         1         1
----------------------------------------------------------------

    variable |      mean         N       max       min       p50
-------------+--------------------------------------------------
 telephonesh |  1.929293        99         2         1         2
----------------------------------------------------------------

    variable |      mean         N       max       min       p50
-------------+--------------------------------------------------
telephones~o |  4.428571         7        20         1         2
----------------------------------------------------------------


But what I wanted to do was actually producing following results by loop;


    variable |      mean         N       max       min       p50
-------------+--------------------------------------------------
   telephone |  1.960888      2659         2         1         2
 telephoneno |      1.25       104         5         1         1
 telephonesh |  1.929293        99         2         1         2
telephones~o |  4.428571         7        20         1         2
----------------------------------------------------------------


    variable |      mean         N       max       min       p50
-------------+--------------------------------------------------
       clock |  1.285446      2659         2         1         1
     clockno |  1.996839      1898        15         1         2
----------------------------------------------------------------

i.e. displaying the results from tabstat by the set of variables that
shares the same prefix.
Of course, the above examples were achieved by writing a single
command line for each group.
But as I have many such variables, I would somehow want to do this
with foreach command(s).

Yusuke


At 06:30 PM 9/28/2006, you wrote:
>I am not sure whether I understand exactly what you want to do, but
>this may help.
>
>1) You could loop over variable lists, along the following lines:
>
>foreach first of varlist telephone* clock* cltv* bicycle* {
>  tabstat `first', s(mean n max min med) c(s)
>}
>
>
>2) If the order matters, you could write a more complicated loop,
>maybe like this:
>
>foreach first of varlist telephone clock cltv bicycle {
>  foreach second in no sh shno {
>   tabstat `first', s(mean n max min med) c(s)
>   capture tabstat `first'`second', s(mean n max min med) c(s)
>}
>}
>
>(The capture is necessary because not all variables are included
>with the same suffixes).
>
>I don't know what you are trying to achieve with this, but there may
>be more straightforward ways to do this. -collapse- and -reshape-
>may be useful for you (again, depending on what you want to do).
>
>HTH,
>Philipp
>
>
>Yusuke Taishi wrote:
>>I have a data set that contains variables like;
>>telephone
>>telephoneno
>>telephonesh
>>telephoneshno
>>clock
>>clockno
>>cltv
>>cltvno
>>bicycle
>>bicycleno
>>bicyclesh
>>bicycleshno
>>and many more.  As you can see, they take the form of ;
>>"name of item"
>>"name of item"no; or
>>["name of item"sh / "name of item"shno, whenever appropriate].
>>I would like to tell STATA to look at the names of variables and execute;
>>tabstat `var', s(mean n max min med) c(s)
>>only for the set of variables that contains the same prefix, and
>>loop over the
>>next set of variables, and then next, and so on.
>>I tried to embed ifcmd or subinstr command in the loop but haven't been able
>>to pull it off. I feel like I'm missing out something very basic.
>>Thanks in advance for your help.
>>Yusuke
>>*
>>*   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/

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



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