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

Hello! I am new to SAS macro and would appreciate your help. 

 

I have the following code that I would like to put into Macro to be able to run with different parameters. 

 

DATA myData;
set myData;

ratio1 = freq1/N1;
ratio2 = freq2/N2;

 

Label
    ratio1 = '% Label 1'
    ratio2 = '% Label 2'
;

RUN;

***************************************

I would like to create a macro function that has the following parameters:

1) Data set

2) Array of Labels (Label 1, Label 2...) with the dynamic length

 

So, I would like to be able to do something like: 

%MACRO calculateRatio (dataSet, lbls);

DATA &dataSet;
 set &dataSet;

 %do i=1 %to %dim(lbls);
        ratio&i =freq&i/N&i ;
%end;

Label
        %do i=1 %to %dim(lbls);
        ratio&i = %:_&&lbls&i;
%end;
;
RUN;

%MEND calculateRatio;

***********

 

%ARRAY (lbls, VALUES=Label1 Label2);
%calculateRatio (myData, lbls);

 

Till now – no luck. I also tried %DO_OVER(lbls,PHRASE=ratio?_I_ = ?) but keep getting tons of syntax errors. The code doesn't work and I am afraid I am doing something basically wrong. The help is highly appreciated!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Pass the list of labels as a single parameter that is delimited by some character.

%macro calculateRatio (inds,outds, labels);
%local i ;
data &outds;
  set &indst;
%do i=1 %to %sysfunc(countw(&labels,|));
    ratio&i =freq&i/N&i ; 
    label ratio&i = %sysfunc(quote(%qscan(&labels,&i,|)));
%end;
run;
%mend calculateRatio;

%calculateratio(inds=have,outds=want,labels=Label1|Label2|Label3)

 

 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You are confusing and combining ARRAY commands with MACRO commands, can't be done.

 

You need to use MACRO commands when you are performing macro functions. The fact that loops can be EITHER arrays or macros doesn't mean you can use array command as macro commands. There is no %dim macro command, dim is an array command. The code below assumes in &lbls you have a delimiter of backslash to separate the labels.

 

UNTESTED CODE

 

%MACRO calculateRatio (dataSet, lbls);
DATA &dataSet;
 set &dataSet;
 %do i=1 %to %sysfunc(countw(%superq(lbls),\));
        ratio&i =freq&i/N&i ;
%end;

Label
        %do i=1 %to %sysfunc(countw(%superq(lbls),\));
        ratio&i = "%str(%%) %scan(&lbls,&i,\)"
%end;
;
RUN;

%MEND calculateRatio;

%calculateRatio(myData,Label 1\Label 2)

 

 

--
Paige Miller
braverju
Obsidian | Level 7
Thank you so much for the explanations! I got it. I was confused indeed. And it worked now. Thank you!
Tom
Super User Tom
Super User

Pass the list of labels as a single parameter that is delimited by some character.

%macro calculateRatio (inds,outds, labels);
%local i ;
data &outds;
  set &indst;
%do i=1 %to %sysfunc(countw(&labels,|));
    ratio&i =freq&i/N&i ; 
    label ratio&i = %sysfunc(quote(%qscan(&labels,&i,|)));
%end;
run;
%mend calculateRatio;

%calculateratio(inds=have,outds=want,labels=Label1|Label2|Label3)

 

 

braverju
Obsidian | Level 7
Thank you so so much! It worked like a miracle.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 5072 views
  • 1 like
  • 3 in conversation