Hi,
I want to apply logic while creating a macro as below:
If %let year=202004 then %let year1 should be 202001 and %let year2 should be 202003
But if %let year=202001 then %let year1 should be 201910 and %let year should be 201912
How to make this kind of macros?
Thanks,
Harsh
From SAS 9.4M5, you can do this in open code
%let year = 202004;
%if &year. = 202004 %then %do;
%let year1 = 202001;
%let year2 = 202003;
%end;
%if &year. = 202001 %then %do;
%let year1 = 201910;
%let year2 = 201912;
%end;
%else %do; %end;
%put &=year1. &=year2.;
What version of SAS are you in?
From SAS 9.4M5, you can do this in open code
%let year = 202004;
%if &year. = 202004 %then %do;
%let year1 = 202001;
%let year2 = 202003;
%end;
%if &year. = 202001 %then %do;
%let year1 = 201910;
%let year2 = 201912;
%end;
%else %do; %end;
%put &=year1. &=year2.;
Thanks,
It is working fine
Great. Glad you found your answer.
Hi,
This works fine for me
But suppose if i have a multiple years columns like 201901 201902 201903 202001 202002 202003
Now if i want to write conditions in %let year=201901 similar for 02 and 03
That if last two digits contains 01 then %let year1 should be 201810 and %let year2 should be 201812
if i want to write conditions in %let year=202001
That if last two digits contains 01 then %let year1 should be 201910 and %let year2 should be 201912
i want to use substr or any function which will help me, i dont know
Could you please help me on this?
Whenever programmers want to create many macro variables based on data logic, I always get a bit skeptical 🙂
Why do you want to do this? This seems to be a part of a larger problem for which there is a better solution.
Hi,
I got a solution,
Thanks,
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.