BookmarkSubscribeRSS Feed
Bal23
Lapis Lazuli | Level 10
%macro uo;
%do i = 1 %to 9;
data a.nc;
%let setname = %scan(9 8 7 6 5 4 3 2 1, &i);
%if &i = 1 %then %do;
update a.data&setname  (keep = v1 v2)
       a.data10        (keep = v1 v2); 
%end;
%else %if &i > 1 %then %do;
update a.nc
	   a.data&setname (keep = v1 v2);
%end;
by v1;
run;
%end;
%mend;

%uo;

I am studying some sas programs and cannot understand the %let, %scan. Can anybody help to explain in details.

 

Thanks a lot

3 REPLIES 3
arodriguez
Lapis Lazuli | Level 10
In macro language, %let is use to set the value of a variable into something.
For example,
%let i=1;
will set the macro variable i to 1, so when you write &i. will be replaced by 1.
%scan do the same that a scan function in a data step. In that case, select the &i word, separated by default separators.
Tom
Super User Tom
Super User

Looks like they want SETNAME to go from 9 down to 1. Instead of the using that strange %SCAN() funciton they could have just used arithmetic to convert I to SETNAME. 

%let setname=%eval(10-&i);

They could have alos just use SETNAME in the %DO loop direcly.

%do setname = 9 %to 1 %by -1;
data a.nc;
%if &setname=9 %then %do;
update a.data&setname  (keep = v1 v2)
       a.data10        (keep = v1 v2); 
%end;
%else %do;
update a.nc
	   a.data&setname (keep = v1 v2);
%end;
by v1;
run;
%end;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 2125 views
  • 3 likes
  • 4 in conversation