DATA Step, Macro, Functions and more

Automate IF-ELSE statement inside a macro.

Reply
Contributor
Posts: 24

Automate IF-ELSE statement inside a macro.

Hello Everyone,

I have a following code snippet, in which we keep adding an ELSE-IF statement whenever a new month starts.

For example: once the month of June will start, a new ELSE-IF condition will be added as follows:


ELSE IF &YYYYMMDD=20150531 THEN FILEDD=20150601;

where: YYYYMMDD will be the previous month's end date & FILEDD is the new month (here June) start date.

I want to automate it & avoid adding the ELSE IF statement manually every month. Any suggestions are welcome. Thanks in advance Smiley Happy

data _null_;

call symputx('tt1',put(intnx('day',today(),-2),yymmddn8.),G);

run;

%macro DAILY_(YYYYMMDD);

data ORCL.BASE1&YYYYMMDD;

set MY_DIR.Daily_&YYYYMMDD.;

IF &YYYYMMDD='20141231'd THEN FILEDD='20150101'd;

else IF &YYYYMMDD='20150131'd THEN FILEDD='20150201'd;

else IF &YYYYMMDD='20150228'd THEN FILEDD='20150301'd;

else IF &YYYYMMDD='20150331'd THEN FILEDD='20150401'd;

else IF &YYYYMMDD='20150430'd THEN FILEDD='20150501'd;

ELSE FILEDD=FILEDD;

%mend;

%DAILY_(&tt1);

Super User
Posts: 7,757

Re: Automate IF-ELSE statement inside a macro.

So you want to set any date that is the last of a month to the first of the following month?

And the dates are stored as numbers in the way you posted, but not as SAS date values?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 24

Re: Automate IF-ELSE statement inside a macro.

Posted in reply to KurtBremser

Hi KurtBremser,

My requirement is, suppose today is 1st June 2015 so whenever a new month will start, I'll add an else if statement like


ELSE IF &YYYYMMDD=20150531 THEN FILEDD=20150601;


& yes the numbers are stored in the format I have posted, these are not SAS date values.

Super User
Posts: 7,757

Re: Automate IF-ELSE statement inside a macro.

How about this in the data step:

int_date = intnx('month',input(put(filedd,8.),yymmdd8.),0,'end');

if input(put(int_date,yymmddn8.),8.) = filedd then filedd = input(put(int_date+1,yymmddn8.),8.);

drop int_date;

The code snippet will find any date stored in your format that is the last of a month and replace it with the first of the following month. No need for dynamic code creation.

Of course, the whole operation would be much easier if dates were stored as SAS date values:

if filedd = intnx('month',filedd,0,'end') then filedd = filedd + 1;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
SAS Employee
Posts: 20

Re: Automate IF-ELSE statement inside a macro.

Try and change your macro to this:

%macro DAILY_(YYYYMMDD);

data ORCL.BASE1&YYYYMMDD;

set MY_DIR.Daily_&YYYYMMDD.;

FILEDD=PUT( INPUT("&YYMMDD", YYMMDD8.) + 1,YYMMDDN8.);

%mend;

Regards,

Michael

Contributor
Posts: 24

Re: Automate IF-ELSE statement inside a macro.

Posted in reply to MichaelLarsen

Hi Michael,

Thank you, I will try with the code & update you.

Super User
Super User
Posts: 7,942

Re: Automate IF-ELSE statement inside a macro.

Hi,

Not sure why you even need a macro for that, seems to be pretty straight forward base SAS code, example:

data have;

  format filedd yymmdd8.;

  filedd="01JAN2014"d; output;

  filedd="06JUN2014"d; output;

run;

data daily_%sysfunc(today(),date9.);

  set have;

  format filedd_new yymmdd8.;

  filedd_new=put(intnx('month',filedd,-1,"END"),yymmddn8.); /* Replace today() with your date value */

run;

Super Contributor
Posts: 435

Re: Automate IF-ELSE statement inside a macro.

Your code produces following values when I ran your code without any change. Is it correct?

fileddfiledd_new
14-01-01********
14-06-06********
Super User
Super User
Posts: 7,942

Re: Automate IF-ELSE statement inside a macro.

Are you running the exact code I provided and looking at the dataset as I see:

filedd          filedd_new

14-01-01     20131231

14-06-06     20140531

It looks to me like you are print the result somehow (**** means its too long for the print column).

Super User
Posts: 7,757

Re: Automate IF-ELSE statement inside a macro.

babloo is right. The statement

format filedd_new yymmdd8.;

caused filedd_new to be defined numeric, which caused an improper conversion in the assignment. When the format is omitted, filedd_new is implicitly defined as character and holds the correct value.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,942

Re: Automate IF-ELSE statement inside a macro.

Posted in reply to KurtBremser

Ah yes.  Should have looked like this:

data have;

  format filedd yymmdd8.;

  filedd="01JAN2014"d; output;

  filedd="06JUN2014"d; output;

run;

data daily_%sysfunc(today(),date9.);

  set have;

  format filedd_new date9.;

  filedd_new=intnx('month',filedd,-1,"END");

run;

Contributor
Posts: 27

Re: Automate IF-ELSE statement inside a macro.

%let YYYYMMDD="11MAY2015"d;

data need;

if &YYYYMMDD=intnx("month",today(),0,"e") then 

need=input(put(intnx("month",&YYYYMMDD,1,"b"),yymmddn8.),8.);

run;

proc print;

run;

Contributor
Posts: 24

Re: Automate IF-ELSE statement inside a macro.

Hello Everyone,

Thanks for your responses, please feel free to change the date format. I'm editiing it in the question itself.

Super User
Posts: 7,757

Re: Automate IF-ELSE statement inside a macro.

data _null_;

refdate = intnx('day',today(),-2);

call symput('YYYYMMDD',put(refdate,yymmddn8.));

call symput('refdate',put(refdate,best6.);

run;


data ORCL.BASE1&YYYYMMDD;

set MY_DIR.Daily_&YYYYMMDD.;

if &refdate = intnx('month',&refdate,0,'end')

then filedd = &refdate + 1;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 30

Re: Automate IF-ELSE statement inside a macro.

Hi All ,

I have a question can anybody explain what the below part is doing and its requirement  and why we have taken (-2 ).

call symputx('tt1',put(intnx('day',today(),-2),yymmddn8.),G);


Thanks

Ask a Question
Discussion stats
  • 16 replies
  • 601 views
  • 7 likes
  • 7 in conversation