DATA Step, Macro, Functions and more

array inside macro

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

array inside macro

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

;

 


Accepted Solutions
Solution
‎08-05-2016 06:23 PM
Super User
Posts: 19,877

Re: array inside macro

Posted in reply to Xiaoningdemao

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


All Replies
Solution
‎08-05-2016 06:23 PM
Super User
Posts: 19,877

Re: array inside macro

Posted in reply to Xiaoningdemao

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

 

Frequent Contributor
Posts: 76

Re: array inside macro

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.
Super User
Posts: 19,877

Re: array inside macro

Posted in reply to Xiaoningdemao

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

 

Frequent Contributor
Posts: 76

Re: array inside macro

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.
Super User
Posts: 19,877

Re: array inside macro

Posted in reply to Xiaoningdemao

No, case only matters in working with character variables.

Frequent Contributor
Posts: 76

Re: array inside macro

Oh~~ That's right.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 351 views
  • 1 like
  • 2 in conversation