help needed for sas macro

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

help needed for sas macro

Dear SAS folks,

I have more than 400 variables and want check the distribution of data for each of them. I developed a piece of code and found that it didn't work. The code is as following:

 

%macro test;
%array abc{406} &var.;
%do i=1 %to 406;
proc sql;
create table abc{i} as
select abc{i}
from monthend.final_201509
;
quit;
%end;
%mend;

Any suggestions?

Thanks,

Harry


Accepted Solutions
Solution
‎11-18-2016 03:43 PM
Super User
Posts: 19,157

Re: help needed for sas macro

What do you mean by check the distribution?

 

Why not something as simple as below. It generates all the summaries for all numeric variables in the dataset by default. 

 

 

proc univariate data=sashelp.class;
histogram;
run;

 

Your macro should be something like the following, but then you need to repeat whatever your analysis is for each table. So then you need another macro, which probably leads to another macro and you're down the road to code you can't reuse or understand.

%macro distribution(varlist=);
%let i=1;
%do %while (%scan(&varlist, &i) ^=%str());
%let var=%scan(&varlist, &i); 


proc sql;
create table &var as
select var
from monthend.final_201509
;
quit;
 
*Increment counter;
%let i=%eval(&i+1);
%end;
%mend;

%distribution( varlist=var1  var2 var3);

 

View solution in original post


All Replies
Solution
‎11-18-2016 03:43 PM
Super User
Posts: 19,157

Re: help needed for sas macro

What do you mean by check the distribution?

 

Why not something as simple as below. It generates all the summaries for all numeric variables in the dataset by default. 

 

 

proc univariate data=sashelp.class;
histogram;
run;

 

Your macro should be something like the following, but then you need to repeat whatever your analysis is for each table. So then you need another macro, which probably leads to another macro and you're down the road to code you can't reuse or understand.

%macro distribution(varlist=);
%let i=1;
%do %while (%scan(&varlist, &i) ^=%str());
%let var=%scan(&varlist, &i); 


proc sql;
create table &var as
select var
from monthend.final_201509
;
quit;
 
*Increment counter;
%let i=%eval(&i+1);
%end;
%mend;

%distribution( varlist=var1  var2 var3);

 

Contributor
Posts: 23

Re: help needed for sas macro

Thanks Reeza a lot for your help. Your code works great. I am just curious why my code didn't work. My logic looks ok, but ....

Super User
Posts: 19,157

Re: help needed for sas macro

The logic is ok, in terms of concepts and pseudocode, but it's not valid SAS synax.

 

The concept of creating a list and accessing the components one at a time is correct., but there is no concept of an array in macro's, so you can't refer to them as abc(i).

 

One workaround is to create them as a list and then access one at a time using Scan to parse the list - which is what my solution implements. This is the same as your logic. 

Contributor
Posts: 23

Re: help needed for sas macro

Got it. Thanks again for detailed explanation.

Best,

Harry

Super User
Posts: 11,134

Re: help needed for sas macro

I suspect that by "didn't work" in this case you meant there were errors. It is very helpful when discussing errors to post results from the LOG including the code and all error messages into an entry box opened by using the "run" icon in the forum menu. The position of some elements of the error messages is important and the entry box will preserve line formatting that the main box of the message loses.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 303 views
  • 0 likes
  • 3 in conversation