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 😊
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.
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?
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.
@s_lassen
It is working Thank you a lot! 😁
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.