Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Use of results in variable name


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Use of results in variable name
Date   Thu, 26 May 2011 12:23:56 +0100

The number of variables is built in as c(k), so you can get it at
directly. This is one way to do it _in general_

unab varlist : *
local lastvar : word `c(k)' of `varlist'
su `lastvar', meanonly
di r(mean)

That could be abbreviated

unab varlist : *
su `: word `c(k)' of `varlist'', meanonly
di r(mean)

In your case, there is a short-cut

su v`c(k)', meanonly
di r(mean)

You were close with your chosen path, but -destring- is for variable
contents only.

describe
mean v`r(k)'

would work.

Nick

On Thu, May 26, 2011 at 12:10 PM, alec minnema <alecminnema@hotmail.com> wrote:

> I have to use many datasets of different sizes and I will use a foreach loop to open them.
> Inside each dataset I look up the last variable in the dataset with
> describe and than I want to know the mean of that variable. So the body of the foreach loop has to look like this:
>
>
>
> describe
>
> scalar LastVariable = r(k)
>
> mean(v`destring(LastVariable)')?? (all variables start with a v and then the number of the variable.)
>
>
> How can I tell stata to look up the variable vX where X is the number saved in LastVariable.
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index