DATA Step, Macro, Functions and more

Two Macros in Data x; Set y; Statement

Accepted Solution Solved
Reply
Valued Guide
Posts: 619
Accepted Solution

Two Macros in Data x; Set y; Statement

Is there something I need to do to use two macro variables in a row dynamically?

%let yearVar = 1617;

%let yearTypeVar = 'S';

 

This works:

data year&yearVar; set work.combined2;
run;

 

Using the second variable causes

data year&yearVar&yearTypeVar; set work.combined2;
run;

ERROR: User does not have appropriate authorization level for library WC000001.

 


Accepted Solutions
Solution
‎02-14-2018 04:01 PM
Super User
Posts: 23,763

Re: Two Macros in Data x; Set y; Statement

Posted in reply to DavidPhillips2

data year&yearVar&yearTypeVar; 

 

How does SAS know where the macro variable ends?

 

Make sure to end it with a period when using them in text or back to back.

 

data year&yearVar.&yearTypeVar.; 

 

In general, using a period at the end always is going to lead to less errors than leaving it out.

PS. Yes, I know people on here will disagree with that last statement Smiley Happy

View solution in original post


All Replies
Super User
Posts: 6,785

Re: Two Macros in Data x; Set y; Statement

[ Edited ]
Posted in reply to DavidPhillips2

Resolving the macro variables shows that your proposed data set name is illegal:

 

data year1617'S'; set work.combined2;

 

If you intended that your data set name should be:

 

year1617S

 

The usual way to achieve that would be to omit the quotes when assigning a value:

 

%let yearTypeVar = S;

 

Then in sections of the code where you need quotes, refer to the macro variable within double-quotes:

 

"&yearTypeVar"

Solution
‎02-14-2018 04:01 PM
Super User
Posts: 23,763

Re: Two Macros in Data x; Set y; Statement

Posted in reply to DavidPhillips2

data year&yearVar&yearTypeVar; 

 

How does SAS know where the macro variable ends?

 

Make sure to end it with a period when using them in text or back to back.

 

data year&yearVar.&yearTypeVar.; 

 

In general, using a period at the end always is going to lead to less errors than leaving it out.

PS. Yes, I know people on here will disagree with that last statement Smiley Happy

Highlighted
Super User
Posts: 6,785

Re: Two Macros in Data x; Set y; Statement

There's nothing wrong with adding a period at the end of macro variable names.  However, it's not the source of the problem here, and it won't change the error condition in this particular case.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 166 views
  • 2 likes
  • 3 in conversation