Hi,
I got the following code (simplified):
%MACRO test(y) ;
year_&y. = 'hello' ;
%MEND ;
DATA work.kip ;
CurrentYear = 2022 ;
%test(CurrentYear) ;
RUN ;
What I get is that the macro creates a variable called "year_y" with the content "hello".
But what I would like to get is that the macro creates a variable called "year_2022" with the content "hello".
Is there anyway of doing this?
Thierry
Create a variable called YEAR, with a value of 2022, not YEAR_2022.
Then when you transpose use the PREFIX option to add the YEAR_ in front of the year. You can use the IDLABEL to have it displayed as 2022 in reports as well rather than YEAR_2022 in reports.
%test(CurrentYear) ;
Macros (like %TEST) cannot access data step variables such as CURRENTYEAR.
Perhaps this is what you want:
%let currentyear=%sysfunc(datepart(%sysfunc(today())));
data kip ;
%test(¤tyear)
run;
My mistake
use this:
%let currentyear=%sysfunc(year(%sysfunc(today())));
data kip ;
%test(¤tyear)
run;
But, since you @ThierryHerrie have stated this is a simplified version of the problem ... I feel compelled to point out what you are doing is usually considered a poor practice.
Variable names should not contain calendar information such as year or year/month etc. I believe you are going down a path that has a lot of drawbacks. You would be much better have having year as a data step variable, and other columns indicating the value of something in that year.
You cannot change the name of a variable after the data step has already started running.
If you want the name to include the digit string 2022 then call the macro with that string.
DATA work.kip ;
CurrentYear = 2022 ;
%test(2022 ) ;
RUN ;
But why not just do this instead?
DATA work.kip ;
CurrentYear = 2022 ;
Message = 'hello';
RUN ;
Now the YEAR is in a variable and the text string 'hello' is in a variable.
Then you could have dataset where CURRENTYEAR is not a constant (and does not need to be known in advance).
DATA work.kip ;
input CurrentYear Message $20.;
cards;
2022 hello
2023 good-bye
;
Inspired by the comments here I think I will use a transpose to create the column(s) with the year in the variable name.
So first create a (dummy) variable with the value “year_2022” and then use that variable as variable name in the transpose.
@ThierryHerrie wrote:
Inspired by the comments here I think I will use a transpose to create the column(s) with the year in the variable name.
So first create a (dummy) variable with the value “year_2022” and then use that variable as variable name in the transpose.
Well, no
Don't create year_2022 in the first place, and then no need to transpose
Create a variable called YEAR, with a value of 2022, not YEAR_2022.
Then when you transpose use the PREFIX option to add the YEAR_ in front of the year. You can use the IDLABEL to have it displayed as 2022 in reports as well rather than YEAR_2022 in reports.
@ThierryHerrie wrote:
I appreciate your concern, but I will 🙂
It is like a report where you have a column per month, except I would like the data in a table.
Cheers!
Hello, @ThierryHerrie this is a common mistake. You want a report with columns like 2021 and 2022 so you think you have to create a data set with calendar information in the variable names. Not true at all, and even though you CAN do it with calendar information in the variable names, it is not a good approach. The better approach is to have a long data set, with a variable named YEAR containing the actual year, and then use PROC REPORT to create the report.
If you can show us a portion of your data as SAS data step code, and show us the desired report, we can show you better ways to go about this.
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!
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.
Ready to level-up your skills? Choose your own adventure.