Changing Dataset Names using SAS Macros

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

Changing Dataset Names using SAS Macros

Hello all, I am trying to make three datasets from a macro that uses the macro variable name to make the name of the dataset. My issues arises from needing to make multiple datasets from different output and I want to change the name of the dataset so there are a total of three datasets with three different names.

 

Here is my code and an example of what I am trying to do:


%macro tab1 (var);

PROC SORT DATA = main OUT= &var ;
BY &var ;
RUN;

PROC FREQ DATA = &var ;
&POPULATION;
TABLES strata1 * strata2 * trtp * peoyn / nocol nopercent cmh;
by &var;
output cmh relrisk out= &var; /*<----- want this dataset to be renamed &var1*/
run;

ODS TRACE ON;
ODS OUTPUT ParameterEstimates= &var; /*<----- want this dataset to be renamed &var_INT*/
proc logistic data=&var;
STRATA strata1 strata2;
&POPULATION;
class trtpn &var;
model peoynn = trtpn &var trtpn*&var;
run;
ODS TRACE OFF;

%mend;

%tab1 (variable_a);
%tab1 (variable_b);

 

 

The product will be six datasets named: (variable_a, variable_a1, variable_a_INT) and (variable_b, variable_b1, variable_b_INT).

 

 

Thank you for your help and let me know if youhave any questions,

 

Best,

 

Donald


Accepted Solutions
Solution
‎06-30-2016 10:01 AM
Super User
Posts: 7,386

Re: Changing Dataset Names using SAS Macros

If you want to use a macro variable as a prefix in a word, you must end the macro variable reference with a dot.

&var1 will make SAS look for the macro variable var1 (which probably fails), while &var.1 will create xxx1 if var contained xxx

yyy&var1.z would then create yyyxxxz

You would therefore use

output cmh relrisk out= &var.1;

and

ODS OUTPUT ParameterEstimates= &var._INT;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎06-30-2016 10:01 AM
Super User
Posts: 7,386

Re: Changing Dataset Names using SAS Macros

If you want to use a macro variable as a prefix in a word, you must end the macro variable reference with a dot.

&var1 will make SAS look for the macro variable var1 (which probably fails), while &var.1 will create xxx1 if var contained xxx

yyy&var1.z would then create yyyxxxz

You would therefore use

output cmh relrisk out= &var.1;

and

ODS OUTPUT ParameterEstimates= &var._INT;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 76

Re: Changing Dataset Names using SAS Macros

Hello KurtBremser,

 

 

This worked like a charm!! I defienlty have alot more to learn about macro programming.

 

Thank you very much for your assistance,

 

Best,

 

Donald

Super User
Posts: 19,034

Re: Changing Dataset Names using SAS Macros

Suggestion, use a prefix to differentiate datasets, not a suffix. SAS allows shortcut of reference names by prefixes, so later on this can be useful. 

 

Ie append together all datasets starting with INT can be done via :

 

data int_master;

set int_:;

run;

Frequent Contributor
Posts: 76

Re: Changing Dataset Names using SAS Macros

Hello Reeza,

 

An excellent and very useful idea. Thank you for this advice, I will incorprate this structure into my programming.

 

Best,

 

Donald

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 422 views
  • 2 likes
  • 3 in conversation