BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
harshpatel
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

What version of SAS are you in?

PeterClemmensen
Tourmaline | Level 20

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.;
harshpatel
Quartz | Level 8

Thanks,

It is working fine

PeterClemmensen
Tourmaline | Level 20

Great. Glad you found your answer.

harshpatel
Quartz | Level 8

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?

 

PeterClemmensen
Tourmaline | Level 20

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.

harshpatel
Quartz | Level 8

Hi,

I got a solution,

Thanks,

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 711 views
  • 0 likes
  • 2 in conversation