BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
elwayfan446
Barite | Level 11

Hello,

 

I am trying to use an explicit date I put into a macro variable to name columns with proc sql;.  I keep getting an error I can't figure out.

 

 

%let currentdatadate=31MAY2022

select
     datefield as &currentdatadate._NEXT_DUE_DATE  format=mmddyy10.
from table
;
quit;

I am getting the following error:

 

elwayfan446_0-1663249270981.png

 

Being able to name the columns dynamically would be ideal.  Hoping someone can help me figure this out.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

When you use a macro variable like &currentdatadate, and then run the program, SAS replaces the macro variable with its value. After this replacement you MUST have legal valid working SAS code and you do not have this. You are trying to name a variable as 31MAY2022_NEXT_DUE_DATE, this is not a valid variable name. Can you figure out why this is not valid?

 

Also, @elwayfan446 , in the future, DO NOT show us partial logs. Show us the ENTIRE log for the PROC or DATA step that has the error. Please do this every single time. Do not make us ask repeatedly.


Also, @elwayfan446 , do not show us code that is different than what you actually ran. The code you showed us is missing a semi-colon and is missing PROC SQL and possibly other things. Show us the code you actually ran from now on.

 

--
Paige Miller

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

The name of a variable in a SAS data set can not begin with a letter.

PaigeMiller
Diamond | Level 26

When you use a macro variable like &currentdatadate, and then run the program, SAS replaces the macro variable with its value. After this replacement you MUST have legal valid working SAS code and you do not have this. You are trying to name a variable as 31MAY2022_NEXT_DUE_DATE, this is not a valid variable name. Can you figure out why this is not valid?

 

Also, @elwayfan446 , in the future, DO NOT show us partial logs. Show us the ENTIRE log for the PROC or DATA step that has the error. Please do this every single time. Do not make us ask repeatedly.


Also, @elwayfan446 , do not show us code that is different than what you actually ran. The code you showed us is missing a semi-colon and is missing PROC SQL and possibly other things. Show us the code you actually ran from now on.

 

--
Paige Miller
elwayfan446
Barite | Level 11

@PaigeMiller 

 

I didn't realize that a macro resolved in a way that would make the variable name invalid when it resolved because of the date being in front.  I moved the date to the back end of the name and it worked well.

 

In regards to the log, I shared the part that was relevant to the issue.  I work for a company where information in the log could and often is not allowed to be shared.  If I am missing something in the log that anyone here feels necessary to help, I will do my best to share it if I can.  If it is too hard to answer without the entire log, I'll understand those who don't want to waste their time trying to help.

 

As for this issue, thank you for helping me realize the simple mistake I was making with the date resolving as an incorrect variable name.

PaigeMiller
Diamond | Level 26

@elwayfan446 wrote:

 

I didn't realize that a macro resolved in a way that would make the variable name invalid when it resolved because of the date being in front.  I moved the date to the back end of the name and it worked well.


Not sure I agree with this wording. The macro variable resolved properly. The usage was incorrect, it has nothing to do with macro variables resolving. As I said, you have to be sure that after the macro variable resolves, the resulting SAS code must be legal valid working SAS code.

 

Also, let's straighten out terminology. You are not using a macro. You are using a macro variable. These are not the same.

--
Paige Miller
elwayfan446
Barite | Level 11
Noted.
I would suggest that if you find a question that frustrates you this much in the future... just scroll on by. There are plenty of people that don't mind helping without making it such an issue.
PaigeMiller
Diamond | Level 26

You help yourself get faster and better answers by showing us full logs (if possible), the code you actually used, and by using proper terminology. This helps you, this helps us. If you don't want to help us and help yourself, that's not good.

 

In this specific case, the problem could be solved without the full log, that will not be true in other cases.

--
Paige Miller
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10
Welcome to constraints on variable names in SAS. 🙂
If you want variable names that begin with numbers, you need to change the VALIDVARNAME option to ANY
However, this means you need to work with variable names as named literals, which is something that SAS EG does more nicely than Display Manager...

options validvarname=any;
data test;
input '1x'n;
cards;
1
2
3
;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2179 views
  • 5 likes
  • 4 in conversation