BookmarkSubscribeRSS Feed
Aman4SAS
Obsidian | Level 7

%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?

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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)?

Aman4SAS
Obsidian | Level 7

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Aman4SAS
Obsidian | Level 7

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

"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;

Aman4SAS
Obsidian | Level 7

Thanks, Its new for me, its working

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

jakarman
Barite | Level 11

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

---->-- ja karman --<-----

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1533 views
  • 0 likes
  • 3 in conversation