BookmarkSubscribeRSS Feed
KP12
Fluorite | Level 6

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

16 REPLIES 16
Kurt_Bremser
Super User

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?

KP12
Fluorite | Level 6

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.

Kurt_Bremser
Super User

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;

MichaelLarsen
SAS Employee

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

KP12
Fluorite | Level 6

Hi Michael,

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Babloo
Rhodochrosite | Level 12

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

fileddfiledd_new
14-01-01********
14-06-06********
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

bharathtuppad
Obsidian | Level 7

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

KP12
Fluorite | Level 6

Hello Everyone,

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

Kurt_Bremser
Super User

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;

naveen20jan
Obsidian | Level 7

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 16 replies
  • 3099 views
  • 7 likes
  • 7 in conversation