BookmarkSubscribeRSS Feed
jeffbezos
Calcite | Level 5

Hi, I've assigned two values using %let and wish to use them to select the column 201809 from my table rather than create the value '201809' in my proc sql statement.  Here's the code i have so far: 

 

%let billyr = 2018;
%let billmth = 09;

 

proc sql;

create table test as select prod_cd, &billyr.&billmth. from sometable;

quit;

 

Any help would be greatly appreciated.

 

Thanks in advance,

Jeff!

3 REPLIES 3
Cynthia_sas
Diamond | Level 26
Hi:
In SAS tables, a column name has to start with a letter or underscore. You cannot have a variable named '201809' if "sometable" is a SAS dataset. Can you show the PROC CONTENTS for WORK.SOMETABLE which shows the EXACT column name that you are trying to build with macro variables?

Cynthia
jeffbezos
Calcite | Level 5

Thank you, modified my code to add a character in front of the variable helped fix it.  noob move.


@Cynthia_sas wrote:
Hi:
In SAS tables, a column name has to start with a letter or underscore. You cannot have a variable named '201809' if "sometable" is a SAS dataset. Can you show the PROC CONTENTS for WORK.SOMETABLE which shows the EXACT column name that you are trying to build with macro variables?

Cynthia

 

Tom
Super User Tom
Super User

If you somehow managed to create a variable name that starts with a number then you need use a name literal to reference it.

%let billyr = 2018;
%let billmth = 09;

proc sql;
create table test as
 select prod_cd, "&billyr.&billmth."n as usable_varname
 from sometable
;
quit;

You also need to make sure that VALIDVARNAME option is set to ANY.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 822 views
  • 0 likes
  • 3 in conversation