BookmarkSubscribeRSS Feed
Marzi
Obsidian | Level 7

Dear All;

 

What the general rule for Macro computation?
I searched on the web and found some example which described the Macro variable trough the
%let
%SYSEVALF
%put
, however still is not clear for me the exact rule.

this is what I want to compute:

Do i = 1 to k;
output{i} = %macro A/%macro B;

End;

the output of both A & B is an integer and I need to save the above command in a loop with k run, means finally my output should be  a matrix(array) with k row and 1 column.

 

9 REPLIES 9
Reeza
Super User

Sounds a bit like your trying to use macros as functions - they're not. If you want a function use PROC FCMP. 

 

A do loop as written should be using an array, amd will generate k columns and 1 row, not k rows and 1 column. 

Matices aren't really what you should be aiming for, in SAS Base it's variables and rows. If you want matrix functionality use PROC IML

 

I suspect you don't need a macro but can't say without further information. . 

 

 

Marzi
Obsidian | Level 7
Yes, I may don't need to have a macro, I wonder how I can use the result of different macro for further computation.
(Unfortunately, I don't have proc IML)
/*macro A*/
%macro A(dataset, Y, X);
%global A;
proc sql ;
select mean(abs(&Y- &X)) format 20.3 into :A from &dataset;
run;
%mend;

/*macro B, the input data for B is different from A*/
%macro B(dataset, Y, X,n);
%global B;
proc sql ;
select sqrt(1-(1-( 1 - mean ( (&Y- &X)**2 )/ mean (( (select mean(&Y) from &dataset)- &X)**2 )))*((count(1) - 1)/(count(1) - 1 - &n))) format 20.3 into :B from &dataset;
run;
%mend;
/*macro A & B, are working correctly, however I have to use the result of booth these two macro in another macro, I need some data preparation for A & B inside the third macro and then run the loop */
%macro C;
array m{*} m1-m&k;
%DO i = 1 %TO &k;
m{i} = %A/%B;
%end;
%mend;
Reeza
Super User

I think it would be beneficial to step back, explain what you're trying to do and post sample input and desired output. 

 

You can probably wrangle your code into working but it but it won't be efficient and even as a learning exercise probably not that useful. You may need a macro but it's hard to tell right now. 

Patrick
Opal | Level 21

SAS macros are mostly used when you want to convert "static" code to "dynamic" code. 

 

My rule of thumb for macros:

1. First make the code work in a static version (=don't use macro coding at all)

2. Only use macro coding if you can't do it with normal Base SAS code

 

If you can clearly tell us what you have (ideally providing a sample data set created via a SAS data step) and then explain us what you need (ideally also posting a desired output/result), then I'm sure someone here in this forum can help you.

 

What's really hard to do, is to give you answers based on some most likely not properly working macro code without any data, any log and not even knowing what you actually want to achieve.

 

 

Astounding
PROC Star

Based on just a quick analaysis, here's a recommendation.

 

First, run %A and %B outside of %C.  That will create &A and &B.  Then inside %C, refer to &A and &B instead of %A and %B.

 

If that doesn't solve the problem, then you're probably looking to do something more complex where %A and %B generate results that vary rather than being constant.  That would require a different approach.

Marzi
Obsidian | Level 7

@Astounding, yes, %A and %B are not constant and within each Do loop their value are changing.

Reeza
Super User

Then if you still need assistance, please explain what you're trying to do and what you have versus what you need. 

 

 

Marzi
Obsidian | Level 7

Thanks @Reeza, somehow, I figure out my question, I replace the macro %A and %B with other methods such that by now, I have a matrix with two columns, then is much easier to divide column 1 by column 2 and save it in the loop.

Reeza
Super User

To answer your initial question as well:

 

%Let -> creates macro variables. There are other ways as well. 

%SYSEVALF -> doing floating point calculations that would require SYSFUNC as well, allows you,to skip one step

%PUT -> displays information in the log

 

These run the gamut of macro statements/functionality so it's hard to see what you're asking 

 

The documentation is fairly thorough and again can't point to specifics without knowing what you're trying to do in more detail. The appendix contains about 13 examples of commonly written macros. 

 

http://support.sas.com/documentation/cdl/en/mcrolref/67912/HTML/default/viewer.htm#p0rmrcca06xibcn1w...

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 1675 views
  • 0 likes
  • 4 in conversation