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: RE: Fwd: foreach loop for generating missing variables


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: Fwd: foreach loop for generating missing variables
Date   Fri, 23 Sep 2011 15:05:50 +0100

That's a good quick solution. I am not sure that any solution not
based on brute force would be better. Here's one.


numlist "100/894"
local all `r(numlist)'
unab exist : x100-x894
local exist : subinstr local exist "x" "", all
local togen : list all - exist

foreach val of local togen {
       gen x`val' = 0
}

This presupposes that x100-x894 is a valid varlist for Andreas's dataset.

Nick

On Fri, Sep 23, 2011 at 8:00 AM, Jesper Lindhardsen
<JESLIN01@geh.regionh.dk> wrote:
> Hi Andreas
>
> This should work, although a crude method:
>
> forvalues val=100/894{
>        capture gen x`val'=.
> }
>
> I must admit that I think your reason(s) for doing this should be
> re-evaluated ;-)
>
> HTH,
>
> Jesper
>
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of andreas chai
> Sent: 23 September 2011 03:16
> To: statalist
> Subject: st: Fwd: foreach loop for generating missing variables
>
> Dear Stata experts :  PLease help!
>
> I have a series of variables "x100 ... x894" in which many of these
> variables are missing. e.g. x240 does not exist
>
> I would like to create these missing variables which contain zero for
> all
> observations.
>
> how do i create these missing variables ?
>
> i have tried writing a loop
>
> !foreach x of newlist  x100- x894{
> !gen `x=92 =3D0
> !}
>
> but keep getting an error as some these variables already exist.
>
>

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