DATA Step, Macro, Functions and more

How to output macro value to a data set?

Reply
New Contributor
Posts: 3

How to output macro value to a data set?

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;

Respected Advisor
Posts: 2,985

Re: How to output macro value to a data set?

[ Edited ]

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
New Contributor
Posts: 3

Re: How to output macro value to a data set?

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
Respected Advisor
Posts: 2,985

Re: How to output macro value to a data set?


@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
Super User
Super User
Posts: 8,073

Re: How to output macro value to a data set?

[ Edited ]

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;

 

 

New Contributor
Posts: 3

Re: How to output macro value to a data set?

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

 

 

Super User
Posts: 10,211

Re: How to output macro value to a data set?

What is the real-world problem you need to solve?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 2,985

Re: How to output macro value to a data set?

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
Ask a Question
Discussion stats
  • 7 replies
  • 153 views
  • 0 likes
  • 4 in conversation