BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
scify
Obsidian | Level 7

Let's say I have five datasets (dat1, dat2, dat3, dat6, and dat8) and I have a few operations I need to apply to each of them. I could, of course, just write out a separate data step for each data set, but I'm wondering if there's a way to combine it into a macro, something like the following:

%macro dostuff;

     %do i in (1 2 3 6 8);

          data dat&i; set dat&i;

               <operations>

              run;

     %end;

%mend;

%dostuff;

Similarly, could I iterate over a series of characters? (e.g. do i in ("horse" "giraffe" "pony" etc...) )

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

%macro oink;

%let list=1 2 3 6 8;

%do i=1 %to %sysfunc(countw(&list));

  %let value=%scan(&list,&i,%str( ));

  %put value &value;

%end;

%mend;

%oink

Works if &list is character or numeric or mixed.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

%macro oink;

%let list=1 2 3 6 8;

%do i=1 %to %sysfunc(countw(&list));

  %let value=%scan(&list,&i,%str( ));

  %put value &value;

%end;

%mend;

%oink

Works if &list is character or numeric or mixed.

--
Paige Miller
art297
Opal | Level 21

Similarly, you can accomplish the same thing by using call execute. e.g.:

data cow horse pig;

  set sashelp.class;

run;

data _null_;

  length excmd $255;

  length dset $32;

  do dset='cow', 'horse', 'pig';

    excmd=catx(' ','data',dset,'; set',dset,';');

    call execute(excmd);

    excmd='newheigt=height-20;';

    call execute(excmd);

    call execute('run;');

  end;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 608 views
  • 3 likes
  • 3 in conversation