Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: RE: counting number of variables in a varlist |
Date | Thu, 16 Jun 2011 06:54:14 +0100 |
Sure. I was having a little joke about -x1 x2-. I don't think there's a best way. Using a global rather than a local is not usually necessary or particularly good style for something you only need in a particular context. Nick On Thu, Jun 16, 2011 at 12:11 AM, ali hashemi <hashemi@vt.edu> wrote: > Many thanks for your help. Actually -x1 x2- was just an example. In the main > problem the number of variables in the varlist is not pre-determined and > depends on the syntax that user uses. So I guess the best way to do it would > be: > > global count: word count `varlist' > > thanks again > Ali > > > After > > . describe <varlist> > > r(k) contains the number of variables in <varlist>. > > After > > unab vars : <varlist> > di `: word count `vars'' > > you get to see how many variables there are. > > If you go > > . di c(k) > > you get to see how many variables there are in memory. > > With -x1 x2- I can count them by hand and get 2 consistently, but this may > not be a skill taught nowadays. > > Nick > n.j.cox@durham.ac.uk > > ali hashemi > > I would like to count the number of variables in a varlist. The way I manage > to do this is as follows: > > local i=0 > foreach var in x1 x2 { > local i=`i'+1 > } > > I was wondering if there is any built-in command which does the same thing. > * * 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/