- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 😊
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@s_lassen
It is working Thank you a lot! 😁
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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