SAS dataset modification by using variable values for the name

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

SAS dataset modification by using variable values for the name

Hi,

 

I'm being tasked with updating a lot of SAS programs due to a change in Dataset name configuration on our servers.

 

Traditonally a series of datasets were stored each month within their own databases. The specific datasets were given the same name within each database. So for example in Freeze_month1 there would be a dataset called Activ. For Freeze_month2 there would be a dataset called activ.

 

This meant that the same code could be run each month just by modifying the libname reference to point to Freeze_month1 or Freeze_month2.

They are now storing all data within the same database but as activ_m1 or activ_m2.

 

I was hoping to create a variable containing the 'month' element of the dataset and append that to the 'activ' element.

 

So the code would look something like

 

libname DB1 odbc dsn='database';

%let mnth ='M1';

 

data aa;

set DB1.activ&mnth;

run;

 

This would then translate to DB1.activ_M1 

 

I've tried using various quotes but without success.

 

Any help would be appreciated as I'm using SAS9.4.

 

Thanks.


Accepted Solutions
Solution
‎10-04-2016 11:13 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: SAS dataset modification by using variable values for the name

You need to examine the text which is generated from using macro, try this:

%let mnth ='M1';

%put db1.activ&mnth;

 

What this will show you is that the code you are generating looks like this:

db1.activ'M1'

 

This is invalid, first the quotes in the macro variable mess it up, and secondly you miss the underscore.  Change to:

%let mnth=M1;

%put db1.activ_&mnth.;

 

And the dataset name will be generated correctly.

View solution in original post


All Replies
Solution
‎10-04-2016 11:13 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: SAS dataset modification by using variable values for the name

You need to examine the text which is generated from using macro, try this:

%let mnth ='M1';

%put db1.activ&mnth;

 

What this will show you is that the code you are generating looks like this:

db1.activ'M1'

 

This is invalid, first the quotes in the macro variable mess it up, and secondly you miss the underscore.  Change to:

%let mnth=M1;

%put db1.activ_&mnth.;

 

And the dataset name will be generated correctly.

Occasional Contributor
Posts: 16

Re: SAS dataset modification by using variable values for the name

That worked perfectly.

 

It was the single quotes that threw me.

 

Thanks very much for such a quick reply

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 223 views
  • 2 likes
  • 2 in conversation