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: about macro's double quotes


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: about macro's double quotes
Date   Tue, 9 Mar 2010 23:56:42 +0100

<>

Several issues are getting mixed up here, and maybe my code is adding to
your confusion, instead of reducing it. So try all your ideas on your own
example, which is what you ultimately want to run... 

Conditioning via an -if- qualifier on the members of the -global- in your
outer loop probably does not do what you expect it to. My example is
designed to highlight this one issue, no more. The second time through the
loop, Stata sees the condition is not fulfilled, and enters missings, just
like you observed. Crucially, it does not discard the entire -generate-
line.

See this even simpler example:

***
clear*
set obs 20
gen x =_n if _n<=10
gen y=2 if _n>20
l
***

The -if- condition for the -gen y- cannot be true, but y still gets created,
and filled with missings...

HTH
Martin


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of G. Dai
Sent: Dienstag, 9. März 2010 23:17
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: RE: about macro's double quotes

thanks, Martin and Nick.
Nick, I did set varabbrev off this time with the example given by Martin,
and still
got xme=. .  It seems STATA still tries to generate and fills xme with . .

On Tue, Mar 9, 2010 at 2:08 PM, Martin Weiss <martin.weiss1@gmx.de> wrote:

>
> <>
>
> BTW, I do not get any complaints from Stata, whether -varabbrev- is on or
> off:
>
>
> *******
> clear*
> set obs 1
> set varabbrev off
> query other
> gen gr5pnhmy=1
> gen gr5pnhm=1
> d
>
> clear*
> set obs 1
> set varabbrev on
> query other
> gen gr5pnhmy=1
> gen gr5pnhm=1
> d
> *******
>
>
> HTH
> Martin
>
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
> Sent: Dienstag, 9. März 2010 22:57
> To: statalist@hsphsun2.harvard.edu
> Subject: RE: st: RE: about macro's double quotes
>
> I already provided an answer. I think that Guang needs to
>
> . set varabbrev off
>
> Stata objects to an instruction to -generate- new variable -gr5pnhm-
> because it interprets that as an abbreviation of existing variable
> -gr5pnhmy-.
>
> Nick
> n.j.cox@durham.ac.uk
>
> Martin Weiss
>
> I would be surprised if Stata indeed complained about "r5pnhm5y", I
> guess it
> complains about "r5pnhm5". Second time through the inner loop, you want
> Stata to refuse to -generate- because the -if- expression is not
> satisfied,
> but Stata does still (try to) -generate-, and fill the new var with
> missings
> - due to the -if-...
>
>
> clear*
> set obs 100
>
> local set "md me"
>
> foreach el in `set'{
>        gen x`el'=1 if "`el'"=="md"
> }
>
> G. Dai
>
> thanks for the reference, Martin.
> yeah, the outer loop is redundant. But I do want to know why that "if"
> doesn't work.  it is wired to a beginner...
>
> On Tue, Mar 9, 2010 at 12:20 PM, Martin Weiss <martin.weiss1@gmx.de>
> wrote:
>
> . If you want to read up on -macro-s, check out manual [U], section
> 18.3.
> >
> > What do you want to achieve with your outer loop? You let it cycle
> through
> > three elements of a -global-. The -gen- and -replace- statements
> inside
> the
> > second loop are supposed to be executed only for the first element of
> the
> > -global-. Would it not be much easier to drag the -gen-/-replace-
> statement
> > out of the inner loop, and ditch the outer one? Maybe this is taken
> out of
> > context, but from what is visible in your post, the outer loop is
> > redundant...
> >
> G. Dai
>
> > I'm a first time  user of macros. As you can see from below, I want to
> > generate a  new var r`i'pnhm when x=md.  After execute the do file, it
> > runs smoothly for x=md. After that, however, I was always reminded by
> STATA
> > that
> >               r5pnhm5y already defined
> >               r(110);
> > so what's wrong with the following program. BTW,I'm little bit
> confused by
> > the macro
> > quotes, can anyone say tips?
>
> > global set "md mom dad";
> > foreach x in $set {;
> > forvalues i=5/8{;
> > gen r`i'pnhm=. if "`x'"=="md";
> > replace r`i'pnhm=int(10*r`i'pnhm5y)/10 if "`x'"=="md";
> > ....
> > };
>
> *
> *   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/
>

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index