LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (May 2010, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 14 May 2010 01:21:10 -0400
Reply-To:     Søren Lassen <s.lassen@POST.TELE.DK>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Søren Lassen <s.lassen@POST.TELE.DK>
Subject:      Re: splitting terms from one macro argument
Comments: To: Jason Schoeneberger <jschoeneberger@CAROLINA.RR.COM>
Content-Type: text/plain; charset=ISO-8859-1

Jason, What you are trying to do is what some people call macro "arrays". The trick is to create the subject variables on the fly:

%macro test(subjects); %local i n w;

%do i=1 %to 9999; %let w=%scan(&subjects,&i); %if %length(&w)=0 %then %goto endscan; %local subject&i; %let subject&i=&w; %end; %endscan: %let n=&i-1;

%do i=1 %to &n; %put subject&i=&&subject&i; %end; %mend;

The trick is to use &&subject&i when refering to the macro variable. It works because the macro processor does several passes, in the first pass "&&" is translated to "&" while "&i" is translated to a number, say 2. In the second pass we have "&subject2", which gets translated to "math" or whatever.

Regards, Søren

On Thu, 13 May 2010 07:09:29 -0400, Jason Schoeneberger <jschoeneberger@CAROLINA.RR.COM> wrote:

>Hi, > > > >I'm putting together a macro and would like to include a parameter where the >user can list multiple subjects separated by a space (e.g read math >science). There will, of course, be different numbers of terms depending on >the user's needs, so I need to be able to identify the number of parameters >submitted. Further, I need to be able to run a piece of code separately for >each one of the subjects, so I'd like to be able to split them out into >separate pieces with their own term. > > > >So in the example above, I would know that it has three subjects and then >I'd like to be able to do something like this: > > > >%let sub1=read; > >%let sub2=math; > >%let sub3=science; > > > >I've followed some threads on the listserv already, but can't quite figure >out how to make it work. I've pulled together the following code, but to no >avail: > > > >%let i=1; > >%do %until (%qscan(&subjects,&i,%str( ))= %str()); > >%let sub=(%qscan(&subjects,&i,%str( )); > >%put &sub; > >%let i=%eval(&i+1); > >%end; > > > >Any direction is appreciated. > > > >Jason


Back to: Top of message | Previous page | Main SAS-L page