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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;

 

 

 

 

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

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;

 

 

 

 

--
Paige Miller

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
  • 1 reply
  • 277 views
  • 4 likes
  • 2 in conversation