I basically want to check if my inputted year (through the macro), is the same as the year for the variable x (a SAS formatted date).
I cam up with two options:
Is this a valid piece of code?
%macro test(year);
x=year(date);
%if &year.=x %then %do;
*blah blah blah;
%end;
What about this next one?
%let j = %eval(x - &year.);
if &j.=0 then do;
*blah blah blah;
end;
Thanks
Macro logic (%IF/%THEN/%ELSE...) are for when you want the macro to conditional output SAS CODE that SAS can then execute.
Once the code is generated and the generated DATA step or PROC is running the macro programming is mainly irrelevant.
So if year(date)=1995, and &year=1995, then the "do" will be executed?
If you want to conditionally execute statements based on whether the DATE variable's value for the current observation being processed is equal to the user specified year then you would code
IF YEAR(DATE) = &YEAR THEN DO;
.....
END;
If the user calls the macro with YEAR set to 1995 ( %test(year=1995) ) then the code above will become :
IF YEAR(DATE) = 1995 THEN DO;
.....
END;
Turn on the MPRINT option so that you can see what code the macro has generated.
I think your mixing up macro code and data step code.
First part the answer is no.
What is X? It isn't in a data set or stored anywhere. You can make it a macro variable. But to evaluate functions not in a datastep you need to use either %eval or %sysfunc depending on what you're trying to do.
The code that is BLAH BLAH must also be valid.
If the second set of code isn't in a datastep then it also isn't valid.
The first one is closest I think, except where does the date come from? Is it a variable because it doesn't look to be in a data step either.
You probably need to provide more details of what you're trying to do.
*if date is a macro variable and this is in macro code;
%if &year.=%sysfunc(year(&date)) %then %do;
or
*if date is a variable and this is in datastep code;
if year(date)=&year then do;
So this is all within a datastep--sorry I should've provided the entire code..
So for this:
if year(date)=&year then do;
Do I not need the %if and %then and %do?
So if year(date)=1995, and &year=1995, then the "do" will be executed?
Macro logic (%IF/%THEN/%ELSE...) are for when you want the macro to conditional output SAS CODE that SAS can then execute.
Once the code is generated and the generated DATA step or PROC is running the macro programming is mainly irrelevant.
So if year(date)=1995, and &year=1995, then the "do" will be executed?
If you want to conditionally execute statements based on whether the DATE variable's value for the current observation being processed is equal to the user specified year then you would code
IF YEAR(DATE) = &YEAR THEN DO;
.....
END;
If the user calls the macro with YEAR set to 1995 ( %test(year=1995) ) then the code above will become :
IF YEAR(DATE) = 1995 THEN DO;
.....
END;
Turn on the MPRINT option so that you can see what code the macro has generated.
To test a variable you need to use actual SAS code. You can reference the macro variable in the code to perform the test.
%macro test(year);
data _null_;
set mydata ;
if year(x) = &year then do;
....
end;
run;
%mend test;
%test(2010);
Now if you want the result of the test to effect the macro logic then you can use CALL SYMPUTX to create a macro variable in that data step that can be used later in the macro to control the generation of other code.
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.