BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
G-Scott
Fluorite | Level 6
Okay, I am sure I am just missing something simple, but I can’t figure it out.

I have a data set where the date is broken down in to three separate variable (month, day, and year), all of which are formatted as Best12.

I am trying to make a macro of the current year and reformat it into best12. for the purpose of exclusion/inclusion.

I know how to use both call symputx and %Let to extract just the year for a macro, but I need to transform it into a numeric with Best12. So when I subset the data I can do it by calling the macro.

I can’t quite figure out how to do that though…any ideas?
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Macro variables are not numeric; the macro language works with text only, and this text is inserted wherever the macro variable is used in code.

So this works:

%let curr_year = 2022;

data want;
set have;
where year = &curr_year.;
run;

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

@G-Scott wrote:

I have a data set where the date is broken down in to three separate variable (month, day, and year), all of which are formatted as Best12.

I am trying to make a macro of the current year and reformat it into best12. for the purpose of exclusion/inclusion.

I tend to be picky about terminology things like this, but you want a macro variable, and not a macro. These two are not the same.

 

Macro variables are not formatted, and they are always 100% of the time character. There's no such things as a numeric macro variable, even though it may look numeric to humans.

 

So you have a year variable in a SAS data set, to make it into a macro variable this is how you do it in a DATA step in SAS:

 

 

call symputx('year',year);

 

No formatting is needed. After the DATA step ends, if you execute this command, you will see that macro variable &year has the proper value.

 

 

 

%put &=year;

 

 

I need to transform it into a numeric with Best12. So when I subset the data I can do it by calling the macro.

 

You can just use &year without any formatting at all. When you subset the data, you are performing a logical (Boolean) operation, something like this DATA step command:

 

if year < &year;

 

and these logical (Boolean) operations (and also arithmetic operations) are ALWAYS (that's 100% of the time, ALWAYS) performed by SAS on the un-formatted values. So not only does it not matter what the format is, but SAS does not use the format in such cases.

--
Paige Miller
G-Scott
Fluorite | Level 6
I appreciate you outlining some of the terminology. That is actually helpful.
Kurt_Bremser
Super User

Macro variables are not numeric; the macro language works with text only, and this text is inserted wherever the macro variable is used in code.

So this works:

%let curr_year = 2022;

data want;
set have;
where year = &curr_year.;
run;

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
  • 3 replies
  • 390 views
  • 0 likes
  • 3 in conversation