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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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