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

Dear SAS experts,

Is there a possible solution to use macro variable as alias when the returned value of macro is number?
I want to rewrite a column name as 202203.

%let date = %sysfunc(today());
%let me = %sysfunc(intnx(month,&date,-1),yymmn6.);
%put &me;

proc sql;
create table final as
select client_id
      ,iznos_rsd as &me
from pom;
quit;

Thank you very much in advance 😊

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

If you want the number as a column value, you will have to refer to it as e.g. "202203"n.

 

So this code will probably work:

%let date = %sysfunc(today());
%let me = %sysfunc(intnx(month,&date,-1),yymmn6.);
%put &me;

proc sql;
create table final as
select client_id
      ,iznos_rsd as "&me"n
from pom;
quit;

but you will have to refer to it the same way in all the programming that follows. This is needed because SAS must know that you are referring to a variable and not a numeric constant.

 

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Why do you want a date as part of a variable name?

Dates are data and do not belong in structure, so this looks like bad data design overall.

What do you intend to do with this column later on?

s_lassen
Meteorite | Level 14

If you want the number as a column value, you will have to refer to it as e.g. "202203"n.

 

So this code will probably work:

%let date = %sysfunc(today());
%let me = %sysfunc(intnx(month,&date,-1),yymmn6.);
%put &me;

proc sql;
create table final as
select client_id
      ,iznos_rsd as "&me"n
from pom;
quit;

but you will have to refer to it the same way in all the programming that follows. This is needed because SAS must know that you are referring to a variable and not a numeric constant.

 

Vidanovic
Fluorite | Level 6

@s_lassen 
It is working Thank you a lot!  😁


PaigeMiller
Diamond | Level 26

When SAS executes code with macro variables, it replaces the macro variable with its value. So your code will run as

 

proc sql;
create table final as
select client_id
      ,iznos_rsd as 202203
from pom;
quit;

which won't work either. This is not legal working SAS code. Can you see why?

 

So, the first thing you need to do is to create working SAS code that does what you want for the date 202203 WITHOUT macro variables. If it doesn't work without macro variables, then it also won't work with macro variables. Many people seem to ignore this advice; don't be one of those people, because your usage of macro variables won't work if you don't first write code that works without macro variables.

--
Paige Miller

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 700 views
  • 1 like
  • 4 in conversation