macro processing question

  • Follow


In SAS base I can use an OR statement or an IN(x1,x2,x3) statement to
represent the union of several values.  Is there an equivalent in
macro language?  I know there is no %OR or a %IN statement.

Specifically I am running a %DO loop but on certain iterations I want
something else executed.

so for instance I have:

%DO i=1 %TO 10;

  data xx&i;
    set x&i;
    %if &i=5 or &i=7 or &i=8 %then %do; /*need help on this line*/
    a=2*(age+10);
    %end;
    %else %do;
    a=age+10;
    %end;
  run;

%end;

Thanks!!!!
0
Reply rlee_at_channing (4) 8/27/2003 3:35:44 PM

Ryna,

I don't understand. This should work exactly as you have it coded. What is
the problem ?

Regards,
Dennis Diskin





Ryan <rlee_at_channing@YAHOO.COM>
Sent by: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
08/27/2003 11:35 AM
Please respond to Ryan


        To:     SAS-L@LISTSERV.UGA.EDU
        cc:
        Subject:        macro processing question


In SAS base I can use an OR statement or an IN(x1,x2,x3) statement to
represent the union of several values.  Is there an equivalent in
macro language?  I know there is no %OR or a %IN statement.

Specifically I am running a %DO loop but on certain iterations I want
something else executed.

so for instance I have:

%DO i=1 %TO 10;

data xx&i;
set x&i;
%if &i=5 or &i=7 or &i=8 %then %do; /*need help on this line*/
a=2*(age+10);
%end;
%else %do;
a=age+10;
%end;
run;

%end;

Thanks!!!!
0
Reply diskin.dennis (215) 8/27/2003 3:51:11 PM


You should be able to use the OR operator, but not the IN. Your code as
written below, however, will probably not produce the results you are
expecting because you are mixing macro statements with data step
statements.  As many have found before you, this does not work well in most
situations because the macro statements are executed when the code is
compiled and the data step statements are executed when the code is
executed.

Greg M. Woolridge
Manager, Study Programming
TAP Pharmaceutical Products Inc.
e-mail: greg.woolridge@tap.com
phone:  847-582-2332
fax:  847-582-2403



                      Ryan
                      <rlee_at_channing         To:      SAS-L@LISTSERV.UGA.EDU
                      @YAHOO.COM>               cc:
                      Sent by: "SAS(r)          Subject: macro processing question
                      Discussion"
                      <SAS-L@LISTSERV.U
                      GA.EDU>


                      08/27/2003 10:35
                      AM
                      Please respond to
                      Ryan






In SAS base I can use an OR statement or an IN(x1,x2,x3) statement to
represent the union of several values.  Is there an equivalent in
macro language?  I know there is no %OR or a %IN statement.

Specifically I am running a %DO loop but on certain iterations I want
something else executed.

so for instance I have:

%DO i=1 %TO 10;

  data xx&i;
    set x&i;
    %if &i=5 or &i=7 or &i=8 %then %do; /*need help on this line*/
    a=2*(age+10);
    %end;
    %else %do;
    a=age+10;
    %end;
  run;

%end;

Thanks!!!!
0
Reply greg.woolridge (76) 8/27/2003 3:56:19 PM

You code is fine as shown. Don't forget to enclose it all in a macro since
you can not use %if, %do etc.as you have done in open code.

"Ryan" <rlee_at_channing@yahoo.com> wrote in message
news:fc030380.0308270735.362d3683@posting.google.com...
> In SAS base I can use an OR statement or an IN(x1,x2,x3) statement to
> represent the union of several values.  Is there an equivalent in
> macro language?  I know there is no %OR or a %IN statement.
>
> Specifically I am running a %DO loop but on certain iterations I want
> something else executed.
>
> so for instance I have:
>
> %DO i=1 %TO 10;
>
>   data xx&i;
>     set x&i;
>     %if &i=5 or &i=7 or &i=8 %then %do; /*need help on this line*/
>     a=2*(age+10);
>     %end;
>     %else %do;
>     a=age+10;
>     %end;
>   run;
>
> %end;
>
> Thanks!!!!


0
Reply roland9898 (130) 8/27/2003 4:15:11 PM

Ryan,

In version 8 you can not use IN(&x1,&x2,&x3), but in some version of version
9 you can.  %IN will be a reserved for

   %do i %in (&x1,&x2,&x3) ;

If I remember correctly, but I don't know when.

Several macros to generate

   &i=5 or &i=7 or &i=8

have been discussed on SAS-L within the last year.  Paul Dorfman probably
introduced one along these lines.

%macro chkin ( var , list ) ;
   %let list = %qsysfunc(compbl(&list)) ;
   %let list = %qsysfunc(tranwrd(&list,%str( ), %nrstr( or &&&var=))) ;
   %unquote(%nrstr(&&&var=)&list)
%mend  chkin ;

/* test code */
%let lk = 6 ;
%put %eval(%chkin ( lk , 1 4 7 )) ;
%let lk = 4 ;
%put %eval(%chkin ( lk , 1 4 7 )) ;

IanWhitlock@westat.com
-----Original Message-----
From: Ryan [mailto:rlee_at_channing@YAHOO.COM]
Sent: Wednesday, August 27, 2003 11:36 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: macro processing question


In SAS base I can use an OR statement or an IN(x1,x2,x3) statement to
represent the union of several values.  Is there an equivalent in macro
language?  I know there is no %OR or a %IN statement.

Specifically I am running a %DO loop but on certain iterations I want
something else executed.

so for instance I have:

%DO i=1 %TO 10;

  data xx&i;
    set x&i;
    %if &i=5 or &i=7 or &i=8 %then %do; /*need help on this line*/
    a=2*(age+10);
    %end;
    %else %do;
    a=age+10;
    %end;
  run;

%end;

Thanks!!!!
0
Reply WHITLOI1 (389) 8/27/2003 4:20:01 PM

Ian,

It has just occurred to me that it may be simpler to avoid ORing (and
associated quoting complexity) altogether by

%macro qin (list= , srch=) ;
   %sysfunc(indexw(&list, &srch))
%mend qin ;

%put %qin (list=1 22 333 4444, srch=333) ;
%put %qin (list=1 22 333 4444, srch=444) ;

Kind regards,
=================
Paul M. Dorfman
Jacksonville, FL
=================


>From: Ian Whitlock <WHITLOI1@WESTAT.COM>
>Reply-To: Ian Whitlock <WHITLOI1@WESTAT.COM>
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: Re: macro processing question
>Date: Wed, 27 Aug 2003 12:20:01 -0400
>
>Ryan,
>
>In version 8 you can not use IN(&x1,&x2,&x3), but in some version of
>version
>9 you can.  %IN will be a reserved for
>
>    %do i %in (&x1,&x2,&x3) ;
>
>If I remember correctly, but I don't know when.
>
>Several macros to generate
>
>    &i=5 or &i=7 or &i=8
>
>have been discussed on SAS-L within the last year.  Paul Dorfman probably
>introduced one along these lines.
>
>%macro chkin ( var , list ) ;
>    %let list = %qsysfunc(compbl(&list)) ;
>    %let list = %qsysfunc(tranwrd(&list,%str( ), %nrstr( or &&&var=))) ;
>    %unquote(%nrstr(&&&var=)&list)
>%mend  chkin ;
>
>/* test code */
>%let lk = 6 ;
>%put %eval(%chkin ( lk , 1 4 7 )) ;
>%let lk = 4 ;
>%put %eval(%chkin ( lk , 1 4 7 )) ;
>
>IanWhitlock@westat.com
>-----Original Message-----
>From: Ryan [mailto:rlee_at_channing@YAHOO.COM]
>Sent: Wednesday, August 27, 2003 11:36 AM
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: macro processing question
>
>
>In SAS base I can use an OR statement or an IN(x1,x2,x3) statement to
>represent the union of several values.  Is there an equivalent in macro
>language?  I know there is no %OR or a %IN statement.
>
>Specifically I am running a %DO loop but on certain iterations I want
>something else executed.
>
>so for instance I have:
>
>%DO i=1 %TO 10;
>
>   data xx&i;
>     set x&i;
>     %if &i=5 or &i=7 or &i=8 %then %do; /*need help on this line*/
>     a=2*(age+10);
>     %end;
>     %else %do;
>     a=age+10;
>     %end;
>   run;
>
>%end;
>
>Thanks!!!!

_________________________________________________________________
Get MSN 8 and help protect your children with advanced parental controls.
http://join.msn.com/?page=features/parental
0
Reply paul_dorfman (183) 8/27/2003 8:17:58 PM

Paul,

But indeed...

Ian

-----Original Message-----
From: Paul Dorfman [mailto:paul_dorfman@HOTMAIL.COM]
Sent: Wednesday, August 27, 2003 4:18 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: macro processing question


Ian,

It has just occurred to me that it may be simpler to avoid ORing (and
associated quoting complexity) altogether by

%macro qin (list= , srch=) ;
   %sysfunc(indexw(&list, &srch))
%mend qin ;

%put %qin (list=1 22 333 4444, srch=333) ;
%put %qin (list=1 22 333 4444, srch=444) ;

Kind regards,
=================
Paul M. Dorfman
Jacksonville, FL
=================


>From: Ian Whitlock <WHITLOI1@WESTAT.COM>
>Reply-To: Ian Whitlock <WHITLOI1@WESTAT.COM>
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: Re: macro processing question
>Date: Wed, 27 Aug 2003 12:20:01 -0400
>
>Ryan,
>
>In version 8 you can not use IN(&x1,&x2,&x3), but in some version of
>version 9 you can.  %IN will be a reserved for
>
>    %do i %in (&x1,&x2,&x3) ;
>
>If I remember correctly, but I don't know when.
>
>Several macros to generate
>
>    &i=5 or &i=7 or &i=8
>
>have been discussed on SAS-L within the last year.  Paul Dorfman
>probably introduced one along these lines.
>
>%macro chkin ( var , list ) ;
>    %let list = %qsysfunc(compbl(&list)) ;
>    %let list = %qsysfunc(tranwrd(&list,%str( ), %nrstr( or &&&var=))) ;
>    %unquote(%nrstr(&&&var=)&list)
>%mend  chkin ;
>
>/* test code */
>%let lk = 6 ;
>%put %eval(%chkin ( lk , 1 4 7 )) ;
>%let lk = 4 ;
>%put %eval(%chkin ( lk , 1 4 7 )) ;
>
>IanWhitlock@westat.com
>-----Original Message-----
>From: Ryan [mailto:rlee_at_channing@YAHOO.COM]
>Sent: Wednesday, August 27, 2003 11:36 AM
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: macro processing question
>
>
>In SAS base I can use an OR statement or an IN(x1,x2,x3) statement to
>represent the union of several values.  Is there an equivalent in macro
>language?  I know there is no %OR or a %IN statement.
>
>Specifically I am running a %DO loop but on certain iterations I want
>something else executed.
>
>so for instance I have:
>
>%DO i=1 %TO 10;
>
>   data xx&i;
>     set x&i;
>     %if &i=5 or &i=7 or &i=8 %then %do; /*need help on this line*/
>     a=2*(age+10);
>     %end;
>     %else %do;
>     a=age+10;
>     %end;
>   run;
>
>%end;
>
>Thanks!!!!

_________________________________________________________________
Get MSN 8 and help protect your children with advanced parental controls.
http://join.msn.com/?page=features/parental
0
Reply WHITLOI1 (389) 8/28/2003 1:06:36 PM

6 Replies
35 Views

(page loaded in 0.131 seconds)


Reply: