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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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.

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26
  %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(&currentyear)
run;

 

 

--
Paige Miller
ThierryHerrie
Obsidian | Level 7
Thanks for your quick reply!
Is there a way to pass 2022 into the parameter of the macro?
PaigeMiller
Diamond | Level 26

My mistake

 

use this:

 

%let currentyear=%sysfunc(year(%sysfunc(today())));

data kip ;
  %test(&currentyear)
run;

 

--
Paige Miller
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ThierryHerrie
Obsidian | Level 7
I agree. Especially when this is something you would like to schedule or something like that.

The endgoal here is to create (for a onetime analysis) a dataset with one row per application and an indicator per month whether the month is relevant or not. This can be one month, or multiple months.
Tom
Super User Tom
Super User

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
;
ThierryHerrie
Obsidian | Level 7

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.

PaigeMiller
Diamond | Level 26

@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

--
Paige Miller
ThierryHerrie
Obsidian | Level 7
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!
Reeza
Super User

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.

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 11 replies
  • 879 views
  • 10 likes
  • 4 in conversation