```Date: Mon, 16 Apr 2001 13:21:54 -0400 Reply-To: Howard Schreier Sender: "SAS(r) Discussion" From: Howard Schreier Subject: Re: Please help with a tricky date calc. Here's a formula-based solution (no loops): data in; informat inDate date9. inMon \$8.; format inDate date9. inMon \$8. LstBusDy weekdate.; input inDate: inMon:; LstAnyDy = intnx( 'month', inDate, mod(month(input(trim(inMon)||'77',monyy.))-month(inDate)+12,12), 'e' ); drop LstAnyDy; LstBusDy = LstAnyDy - max(0,mod(weekday(LstAnyDy)+5,7)-4); cards; 01JUL2000 JUL 01JUL2000 SEP 01DEC2000 FEB 01DEC2001 MAR ; The "77" is arbitrary, since the year stored by the INPUT function is immediately discarded by the MONTH function. Output: Obs inDate inMon LstBusDy 1 01JUL2000 JUL Monday, July 31, 2000 2 01JUL2000 SEP Friday, September 29, 2000 3 01DEC2000 FEB Wednesday, February 28, 2001 4 01DEC2001 MAR Friday, March 29, 2002 On Thu, 12 Apr 2001 09:38:35 -0400, Howard Schreier wrote: >The sample data did not include a target month which ends on Sunday. In my >testing, the Karsten/Philip solution did not handle that. > >Here's a solution which I like because of its relative transparency and the >absence of any intermediate variables: > > data in; > informat inDate date9. inMon \$8.; > format inDate date9. inMon \$8. LstBusDy weekdate.; > input inDate: inMon:; > LstBusDy = intnx( 'month', inDate, 0, 'e' ); > do while (upcase(put(LstBusDy,monname3.)) ne upcase(inMon)); > LstBusDy = intnx( 'month', LstBusDy, 1, 'e' ); > end; > do while (upcase(put(LstBusDy,weekdate3.)) in ('SAT','SUN')); > LstBusDy = LstBusDy - 1; > end; > cards; > 01JUL2000 JUL > 01JUL2000 SEP > 01DEC2000 FEB > 01DEC2001 MAR > ; >On Thu, 12 Apr 2001 14:37:55 +0800, mvyver wrote: > >>Thanks, This is a nice neat solution. >>Cheers >>MV >> >>-----Original Message----- >>From: Philip_Crane@workcover.vic.gov.au >>[mailto:Philip_Crane@workcover.vic.gov.au] >>Sent: Thursday, April 12, 2001 1:31 PM >>To: mvyver >>Subject: Re: Please help with a tricky date calc. >> >> >> >>Mark >>Don't know if this is much help, it just puts some structure arround the >>code >>Karsten supplied which does most of the work. >>data in; >>3 format inDate date9. inMon \$8.; >>4 informat inDate date9. inMon \$8.; >>5 input inDate inMon; >>6 select(inMon); >>7 when('FEB') in_mm = 2; >>8 when('JUL') in_mm = 7; >>9 when('SEP') in_mm = 9; >>10 end; >>11 if in_mm < month(inDate) >>12 then out_dt = mdy(in_mm,1,year(inDate)+1); >>13 else out_dt = mdy(in_mm,1,year(inDate)); >>14 LstBusDy = intnx( 'month', out_dt, 0, 'e' ) - >>15 max(0,weekday( intnx( 'month', date, 0, 'e' )) - 6 ); >>16 cards; >>17 01JUL2000 JUL >>18 01JUL2000 SEP >>19 01DEC2000 FEB >>20 ; >>21 run; >>22 proc print data=in; >>23 var inDate inMon LstBusDy; >>Philip >> >> >> >> >> >> >>mvyver on 12/04/2001 11:51:11 >>Please respond to mvyver >> >>To: SAS-L@LISTSERV.UGA.EDU >>cc: >>Subject: Please help with a tricky date calc. >> >> >> >> >> >> >>Hi, >>I have a tricky date calc which I've been struggling with, and was hoping >to >>get some help. >>I'll try and describe what I'm trying to do succinctly. >>The input data set has a date and a character variable (there is an example >>below): >>inDate=nnnnnnn (date value formatted as date9.) >>inMon=ccc (3 letter value indicating a month; jan feb mar, etc. this month >>may or may not be the same month in inDate, and it may be in the same year >>or the next year, but never the previous year!) >>What I'd like to get is a SAS datevalue representing the last working day >of >>the month, inMon, i.e Monday-Friday - assuming no holidays: >>outLastBusDay=nnnnnnn (date value formatted as date9.) >>I won't bore you with the umpteen variations I've tried. I've looked at the >>SAS web site calendar example, but that wasn't too much help - except for >>showing why I was having such trouble with the intx() function, and how >>complicated it was to create the '01Apr199'D value! >>Any help is appreciated. >>Thanks in advance >>Mark Van De Vyver >>University of Western Australia >>Perth >>data in; >>format inDate date9. inMon \$8.; >>informat inDate date9. inMon \$8.; >>input inDate inMon; >>cards; >>01JUL1994 JUL >>01JUL1994 SEP >>01DEC1994 FEB >>; >>run; ```

Back to: Top of message | Previous page | Main SAS-L page