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-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
  • 456 views
  • 1 like
  • 3 in conversation