Hello
I would like to ask a question please about one statement that need to repeat on multiple variables.
Example:
/*Way1*/
If item1 eq . then item1 = 0 ;
If item2 eq . then item2 = 0 ;
If item3 eq . then item3 = 0 ;
and so on
If item100 eq . then item100 = 0 ;
/*Way2*/
%macro check;
%do i = 1 to 100;
if item&i eq . then item&i = 0;
%end check ; My question is how to do it when there are multiple statements that need to repeat.
for example:
/*Way1*/
If item1 eq . then item1 = 0 ;else IF item1>=500 then item1=500;
If item2 eq . then item2 = 0 ;else IF item2>=500 then item2=500;
If item3 eq . then item3 = 0 ; else IF item3>=500 then item3=500;
and so on
If item100 eq . then item100 = 0 ; else IF item100>=500 then item100=500;But what is the way to do it via macro???
/*Way2*/
%macro check;
%do i = 1 to 100;
if item&i eq . then item&i = 0;
else IF item&i>=500 then item&i=500;
%end check ;
I know how to use macr
When you want to perform the same task(s) on multiple columns, you use ARRAYs in a data step and you would NOT use macros. This is exactly the situation where ARRAYs are meant for.
data want;
set have;
array item item1-item100;
do i=1 to dim(item);
if missing(item(i)) then item(i)=0;
else if item(i)>=500 then item(i)=500;
end;
drop i;
run;
When you want to perform the same task(s) on multiple columns, you use ARRAYs in a data step and you would NOT use macros. This is exactly the situation where ARRAYs are meant for.
data want;
set have;
array item item1-item100;
do i=1 to dim(item);
if missing(item(i)) then item(i)=0;
else if item(i)>=500 then item(i)=500;
end;
drop i;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.