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;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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