Date: Mon, 2 Feb 2004 15:59:35 -0500
Reply-To: "DePuy, Venita" <depuy001@DCRI.DUKE.EDU>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "DePuy, Venita" <depuy001@DCRI.DUKE.EDU>
Subject: Re: %if/%then resolution - shouldn't be true?
Thanks to Dale, Kevin, and Dennis for their replies!
Dale got what I was looking for in a nutshell.
I appreciate all the help!
> From: Dale McLerran[SMTP:firstname.lastname@example.org]
> Sent: Monday, February 02, 2004 1:35 PM
> To: DePuy, Venita; SAS-L@LISTSERV.UGA.EDU
> Subject: Re: %if/%then resolution - shouldn't be true?
> I believe that you are committing that classic mistake of
> confusing data step and macro boundaries. You want to test
> something about your data and, depending on the value of that
> test you want to perform some action. As I understand your
> intention, you want to sort by p-value, and if the largest
> p-value (apart from the test of the intercept) is either missing
> or greater than 0.05, then you want to print the entire set of
> p-values (apart from the test of the intercept). Is that the
> proper description of your problem?
> Before providing code which solves the problem which I stated
> above, let's look at your code.
> %macro file1;
> /* Select all parameters other than intercept*/
> data test1; set test1; where variable ne 'Intercep';
> /* Sort data by p-value */
> proc sort data=test1; by probt;
> %if last.Probt > .05 OR last.Probt = . %then %do;
> /* Note that at this point, you appear to be trying to use */
> /* last. processing within a PROC SORT. That can't be done. */
> /* You would need to initiate a data step to have last. */
> /* processing capabilities available. That is problem #1. */
> proc print;run;
> /* So, what is problem #2? Why does your statement resolve */
> /* to true? Well, you are comparing the value of the string */
> /* last.Probt with the value of the string .05. Remember */
> /* that you are not within a data step here. You are working */
> /* within the context of the macro environment. In the macro */
> /* environment, last.Probt is a string as is .05 The value */
> /* of the string last.Probt is greater than the value of the */
> /* string .05 Thus, your comparison is true every time. */
> Here is code which appropriately solves the problem I stated above:
> %macro file1;
> data test1; set test1; where variable ne 'Intercep'; run;
> proc sort data=test1; by probt; run;
> data _null_; /* Initiate data step */
> set test1 end=lastrec; /* Determine if last ordered p-value */
> if lastrec & (Probt > .05 OR Probt = .) then do;
> /* Last ordered p-value AND p-value>0.5 or p-value=. */
> /* Note that we are testing the value of some datum. */
> call execute("proc print data=test1; run;");
> /* call execute will launch proc print when */
> /* the current datastep is finished. */
> options mprint;
> --- "DePuy, Venita" <depuy001@DCRI.DUKE.EDU> wrote:
> > Hi all -
> > hopefully a simple question.
> > I think this %if/%then statement should resolve to false, instead it
> > comes
> > out as true.
> > (This is a simplified bit of a larger process).
> > Code and partial log follows.
> > Output = both print statements run.
> > Thanks,
> > Venita
> > data test1;
> > input variable $ probt @@;
> > cards;
> > Intercep 0.5 s 0.0026 s_sq 0.0232
> > ;
> > proc print;
> > %macro file1;
> > data test1; set test1; where variable ne 'Intercep';
> > proc sort data=test1; by probt;
> > %if last.Probt > .05 OR last.Probt = . %then %do;
> > proc print;run;
> > %end;
> > %mend;
> > %file1;