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