BookmarkSubscribeRSS Feed
kero1234
Calcite | Level 5

How can I output the macro value to a dataset instead of using put to put to the sas log?

I tried 

 

data rule;

from = &A;

output;

to=&B;

output;

run;

but it seems the recursion of the macro stop at the first iterate.....

 

%MACRO TOH (N,A,B);
%IF &N =1 %THEN %DO;
/* want to output &A and &B to a data set here

 

 

*/

%ELSE %DO;
%TOH(%EVAL (&N-1), &A, %EVAL(6-&A-&B));
%TOH(1,&A,&B);
%TOH(%EVAL(&N-1),%EVAL(6-&A-&B), &B);
%END;
%MEND TOH;

%MACRO TOH2;
%LET A=1;
%LET B=2;
%DO N=1 %TO 5;
data _null_;
put "Number of disk = &N";
%TOH(&N, &A, &B);
PUT "STR( )";
%END;
%MEND TOH2;

7 REPLIES 7
PaigeMiller
Diamond | Level 26

Although I have no idea what you are trying to accomplish here, it seems like all the calculations could be done in a data step instead of a macro (HINT HINT HINT). As far as I know, recursion is not allowed and so you can't have macro TOH call macro TOH.

 

Also, there is no such things as a macro "outputting" a result to a data set, but there's no reason you couldn't put the data step inside the macro and then assign the macro variable values to a data set variable inside the macro.

 

 

--
Paige Miller
kero1234
Calcite | Level 5
Haha... surprise by the comments! Maybe I shd ask in email lol. For the data step inside macro... it only got one observation... seems can’t accumulate the macro value even I use the output function
PaigeMiller
Diamond | Level 26

@kero1234 wrote:
Haha... surprise by the comments! Maybe I shd ask in email lol. For the data step inside macro... it only got one observation... seems can’t accumulate the macro value even I use the output function

Well, maybe it would help, as @Tom asked, if you explain what you are doing.

 

Accumulating values can be done in a data step, there's no need for all this macro complications. Or the data step where the value is outputted to is actually in the macro, then there's no problem creating the data step (just the problem of recursive macros).

 

So ... explain what you are trying to do.

--
Paige Miller
Tom
Super User Tom
Super User

You need to explain more what you are actually trying to do.  

It really helps to write out the SAS code you are trying to create before writing a macro to create it for you.

For example your first data step is very confusing.  If we remove the macro variable references you have written.

data rule;
  from = 1; output;
  to=2;  output;
run;

So that will create a SAS dataset that looks like this

FROM TO
1 .
1 2

Is that what you want?

 

I think you are also confused about how to add observations to a dataset.  The easiest way is to us PROC APPEND. So first create a dataset with the observations you want to append and then use proc append to add the obserations from that dataset to the one you are using the accumulate the results.

data current_obs ;
   from=&A;
   to=&B;
run;
proc append base=all_obs data=current_obs force;
run;

 

 

kero1234
Calcite | Level 5

Thanks @Tom @PaigeMiller

 

So in the original macro TOH, the number of &A and &B given out depends on the N define in TOH2. Let say N=5. there will be 31 combinations of &A and &B.

 

If I use PUT statement to put them in sas log,

%macro TOH (N,A,B);
%IF &N =1 %THEN %DO;
PUT "&A and &B;

 

In sas log it will be like:

 

1 2

1 3

3 2

2 3

1 3

 .

 .

 .

 ........(26 more)

 

However, if it is in sas log I cannot further process it 

I want to save in a data set so I can retrieve later.  I try to store all of them (31 obs) in a dataset but fail to do so

 

 

%macro TOH (N,A,B);
%IF &N =1 %THEN %DO;
data rule_list;

............

 

In the resulting data set, it will be good to have

 a b

1 2

1 3

3 2

2 3

1 3

 

 

PaigeMiller
Diamond | Level 26

Instead of PUT, create a SAS date set, right there in the same spot in the exact SAS code where you had the PUT

 

 I try to store all of them (31 obs) in a dataset but fail to do so

 

You have to show us your code, better yet show us the code and SASLOG. You can't just say "fail to do so" and expect to get meaningful help.

--
Paige Miller

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