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.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

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
  • 930 views
  • 0 likes
  • 3 in conversation