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

Hi, 

I am not familiar with using Macro, but I think macro can really help to generate powerful code in my scenario.

So, I have variable X1_1 to X10_1, each of them has its match Y1_1 to Y10_1. I want to create MI=1 if the condition is matched. 

 

DATA file;

set file;

IF X1_1 in (1:3)  & Y1_1>=70 & Y1_1<=100 then MI=1;

IF X2_1 in (1:3)  & Y2_1>=70 & Y2_1<=100 then MI=1;

...

IF X10_1 in (1:3)  & Y10_1>=70 & Y10_1<=100 then MI=1;

run;

 

I wonder if anyone here can help me to use Macro to create my codes. Thank you so much.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You do not need a macro here, you need an array. 

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

DATA file;
set file;

array x(*) x1_1--x10_1;*assumes variables are in order;
array y(*) y1_1 --y10_1;*assumes variables are in order;

*set to missing;
M1 = .;

do i=1 to 10 while(M1 ne 1);
      if x(i) in (1:3) and y(i) >=70 and y(i) <= 100 then M1 =1;
end;

run;

@AmyH_01 wrote:

Hi, 

I am not familiar with using Macro, but I think macro can really help to generate powerful code in my scenario.

So, I have variable X1_1 to X10_1, each of them has its match Y1_1 to Y10_1. I want to create MI=1 if the condition is matched. 

 

DATA file;

set file;

IF X1_1 in (1:3)  & Y1_1>=70 & Y1_1<=100 then MI=1;

IF X2_1 in (1:3)  & Y2_1>=70 & Y2_1<=100 then MI=1;

...

IF X10_1 in (1:3)  & Y10_1>=70 & Y10_1<=100 then MI=1;

run;

 

I wonder if anyone here can help me to use Macro to create my codes. Thank you so much.

 

 


 

View solution in original post

3 REPLIES 3
Reeza
Super User

You do not need a macro here, you need an array. 

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

DATA file;
set file;

array x(*) x1_1--x10_1;*assumes variables are in order;
array y(*) y1_1 --y10_1;*assumes variables are in order;

*set to missing;
M1 = .;

do i=1 to 10 while(M1 ne 1);
      if x(i) in (1:3) and y(i) >=70 and y(i) <= 100 then M1 =1;
end;

run;

@AmyH_01 wrote:

Hi, 

I am not familiar with using Macro, but I think macro can really help to generate powerful code in my scenario.

So, I have variable X1_1 to X10_1, each of them has its match Y1_1 to Y10_1. I want to create MI=1 if the condition is matched. 

 

DATA file;

set file;

IF X1_1 in (1:3)  & Y1_1>=70 & Y1_1<=100 then MI=1;

IF X2_1 in (1:3)  & Y2_1>=70 & Y2_1<=100 then MI=1;

...

IF X10_1 in (1:3)  & Y10_1>=70 & Y10_1<=100 then MI=1;

run;

 

I wonder if anyone here can help me to use Macro to create my codes. Thank you so much.

 

 


 

ketpt42
Quartz | Level 8

I have just been @Reeza'd. Too fast.

AmyH_01
Calcite | Level 5
Thank you so much, Reeza! I was doing the array at first but I forgot to put while (MI ne 1), that caused the result became a mess. Thank you so much for your great help!! I truly appreciate it!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1245 views
  • 1 like
  • 3 in conversation