DATA Step, Macro, Functions and more

issue with %scan

Reply
Super Contributor
Posts: 266

issue with %scan

%let x= South Dakota, XY|New Jersy, NJ|Minneapolis-St. Paul, MN|Arizona's, AZ

i want to fetch all area one by one using

%macro area();

%do i= 1 %to 4;

%let area= %scan(&x,&i,%str(|));

%put &area;

%end;

%mend;

%area;

can we do that without manipulating original string and get the area name as it is?

Super User
Super User
Posts: 7,942

Re: issue with %scan

Well, this works:

%let x= "South Dakota, XY|New Jersy, NJ|Minneapolis-St. Paul, MN|Arizona's, AZ";
%macro area();
  %do i= 1 %to 4;
    %let area=%qscan(&x,&i,|);
    %put &area;
  %end;
%mend;

%area;

However I would again advise not to do things this way.  Your going to end up with all kinds of issues, differing quotes, space limitations etc.  What is it your trying to achieve (inputs/outputs)?

Super Contributor
Posts: 266

Re: issue with %scan

yes, i agree,

i am able to achieve my output after manipulation in string. but i m looking if there is any macro function with the help of that i can achieve my output.

with ur code, i will get first and last area with double quote. which i dont want , actually i will not get becaz this list is being created  from a dataset.

is there anything else?

Thanks

Super User
Super User
Posts: 7,942

Re: issue with %scan

If the list is being created from a dataset, then why not just generate the code from that dataset and save yourself some headaches:

data have;
     length state $200.;
     infile datalines dlm="¬";
     input state $;
datalines;
South DakotaXY
New Jersy NJ
Minneapolis-St. Paul MN
Arizona's, AZ
;
run;

data _null_;
  set have;
  call execute('%put '||state||';');
run;

Even if your calling another macro with that:

data _null_;

     set have;

     call execute('%some_macro ("'||strip(state)||'");');

run;

Far simpler than messing with lists and list processing in macro code.

Super Contributor
Posts: 266

Re: issue with %scan

My apology if i was not clear with my que.

Actually i have a 15yr time series data of 100's of area.

and i have a dataset AREA ,

i will create a macro variable list of area with separated by "|"

then i will run a do loop to create many type of dashboard.

for that i need to fetch the each area one by one,

Thanks .

* i m lack of knowledge with Call execute

Super User
Super User
Posts: 7,942

Re: issue with %scan

"Actually i have a 15yr time series data of 100's of area" - exactly why I suggest that macro lists are not a good idea.  You have a dataset AREA which contains one observation per area.  Use this to your advantage:

data _null_;

     set AREA;

     call execute('%Create_Dashboard(Area="'||strip(area)||'");');

run;

So instead of the create_dashboard() macro doing the loop itself you use the call execute statement to generate one macro call per observation in AREA, passing in area as the macro parameter.  The macro remains quite simple then, a report example:

%macro Create_Dashboard (Area=);

     proc report data=my_data;

          where area="&AREA.";

          column ...;

          define ...;

     run;

%mend Create_Dashboard;

Super Contributor
Posts: 266

Re: issue with %scan

Thanks, Its new for me, its working

Super User
Super User
Posts: 7,942

Re: issue with %scan

No probs.  Glad it helped.  Once you understand that the datastep can be used like a loop then it removes the need for long macro strings and splitting those out.  The call execute (and there is also call sub in newer SAS editions) are quite powerful.  I use them a fair bit and generate whole programs from metadata - a few lines of code, and a table of metadata can write lots of code out.  For instance my code above could become:

data _null_;

     set area;

     call execute('proc report data=my_data;

                              where area="'||strip(area)||'";

                               ...');

run;

If for New Jersey NJ you only want vara and varb output; but varc for the others:

data _null_;

     set area;

     call execute('proc report data=my_data;

                             where area="'||strip(area)||'";');

     if area ne "New Jersey, NJ" then call execute(' column vara varb varc;...');

     else call execute(' column vara varb; ...');

run;

Trusted Advisor
Posts: 3,212

Re: issue with %scan

Amand4SAS, please review the "by" usage or the way SAS-portal or SAS-VA is doing that. Times are changing.    

---->-- ja karman --<-----
Ask a Question
Discussion stats
  • 8 replies
  • 293 views
  • 0 likes
  • 3 in conversation