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

Dear All,

 

I want to count the number of observations in each row that satisfiy a certain condition. I want o repreat this procedure for many data set so I'm using macro, but I have problem with using array inside marco. I attached a sample data set and the code I wrote.

 

Thank you in advance!!!

 

Best wishes.

 

data varselect1;

input variable1 variable2 variable3;

cards;

15 20 17

15 18 46

4 5 7

9 40 46

;

run;

%let J=3

%macro count;

%do j=1 %to &J;

data count&j;

set varselect&j;

array a{*} _numeric_;

lt15 = 0;

%do _n_=1 %to 3;

%if a{_n_} lt 15 %then lt15+1;

%end;

run;

%end;

%mend count;

%count

;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

If you're in a data step don't use a macro do loop , use a data step do loop, ie no %

 

View solution in original post

6 REPLIES 6
Reeza
Super User

If you're in a data step don't use a macro do loop , use a data step do loop, ie no %

 

Xiaoningdemao
Quartz | Level 8
Dear Reeza,

OK! I see, thank you very much!
Another issue with this code is that, I set J=3 at the beginning, but it kept saying
' File WORK.VARSELECT4.DATA does not exis
',
and each time I ran the code, J seems to be increase by 1. How did this happen?

Thanks again!

Best wishes.
Reeza
Super User

You're using the same macro variable twice, as a counter and loop end.

 

%let j=3;

 

%do j=1 %to &j

 

The macro DO loop creates a new macro J variable. I would definely expect unexpected behaviour with this type of coding. Using a different macro variable will help resolve this issue. 

 

%let end_loop=3;

 

%do j=1 %to &end_loop

 

Xiaoningdemao
Quartz | Level 8
Oh!! SAS doesn't distinguish upper and lower case!! I was a R user, this is how I would code in R......
Thanks again!! and have a nice weekend~~
Best wishes.
Reeza
Super User

No, case only matters in working with character variables.

Xiaoningdemao
Quartz | Level 8
Oh~~ That's right.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 6 replies
  • 2446 views
  • 1 like
  • 2 in conversation