weekday function error in macro

Reply
Contributor LRN
Contributor
Posts: 57

weekday function error in macro

Hi All,

I am trying to create dataset for each day for a month (except for sundays). The weekday part is giving me error. Could someone help me with this.

       

options symbolgen merror mlogic;



data _null_;

sdate=intnx('month',date(),-1);


edate=intnx('month',date(),0)-1;



call symputx('yy',put(sdate,year4.));


call symputx('mm',put(sdate,month2.));


call symputx('si',put(sdate,day2.));


call symputx('ei',put(edate,day2.));



run;



%put &yy;


%put &mm;


%put &si;


%put &ei;



%macro test;


%do i =&si %to &ei;



%do i =&si %to &ei;



%let mon=%sysfunc(putn(&mm,z2.));


%let dt=%sysfunc(putn(&i,z2.));


%let DATE2 =%sysfunc(putn(&yy&mon&dt, yyyymmdd8.));


%put &date2;



%let wkday=%sysfunc(weekday(&date2.));


%put &wkday;



%if &wkday eq 1 %then leave ;


%else %do;


yy.&mon.&dt.;




%end;


%end;


%return;


%end;



%mend;


Thanks

LR

Respected Advisor
Posts: 3,156

Re: weekday function error in macro

Have no clue what you want to do, it seems to me that data step can do the same thing much easier. Anyway, I have fixed several bugs in your code, although I am not sure if it fits your expectation, it at least runs without error.

options symbolgen merror mlogic;

data _null_;

sdate=intnx('month',date(),-1);

edate=intnx('month',date(),0)-1;

call symputx('yy',put(sdate,year4.));

call symputx('mm',put(sdate,month2.));

call symputx('si',put(sdate,day2.));

call symputx('ei',put(edate,day2.));

run;

%put &yy;

%put &mm;

%put &si;

%put &ei;

%macro test;

%do i =&si %to &ei;

%do i =&si %to &ei;

%let mon=%sysfunc(putn(&mm,z2.));

%let dt=%sysfunc(putn(&i,z2.));

%let DATE2 =%sysfunc(inputn(&yy&mon&dt, yymmdd8.));

%put &date2;

%let wkday=%sysfunc(weekday(&date2.));

%put &wkday;

%if &wkday eq 1 %then leave ;

%else %do;

%put &yy.&mon.&dt.;

%end;

%end;

%return;

%end;

%mend;


Regards,

Haikuo

%test

Super User
Super User
Posts: 7,081

Re: weekday function error in macro

Your looping is working way too hard so it is difficult to discern what your actual logic is.  It looks like you are trying to find the first SUNDAY in the month?

So you can find the first day of the month using INTNX().

You can find what day of the week that is by using WEEKDAY().

You can figure out how many days to add to get to the first sunday of the month by using the formula MOD( 8 - weekday, 7 ).  So for a Sunday you add 0 and for a Monday you add 6, etc.

%macro test(date);

%local sdate diff ;

%let sdate = %sysfunc(intnx(month,&date,0,b));

%let diff = %sysfunc(mod(8-%sysfunc(weekday(&sdate)),7)) ;

%sysfunc(putn(&sdate+&diff,yymmddn8.))

%mend test;

%put %test('01JUN2014'd);

20140601

%put %test('23JUL2014'd);

20140706

Ask a Question
Discussion stats
  • 2 replies
  • 728 views
  • 0 likes
  • 3 in conversation