DATA Step, Macro, Functions and more

anybody helps to explain what %let means here and the scan function

Reply
Super Contributor
Posts: 345

anybody helps to explain what %let means here and the scan function

%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

Frequent Contributor
Posts: 144

Re: anybody helps to explain what %let means here and the scan function

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.
SAS Employee
Posts: 106

Re: anybody helps to explain what %let means here and the scan function

Super User
Super User
Posts: 7,039

Re: anybody helps to explain what %let means here and the scan function

[ Edited ]

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;

 

Ask a Question
Discussion stats
  • 3 replies
  • 267 views
  • 3 likes
  • 4 in conversation