Date: Mon, 19 May 2008 14:38:52 -0700
Reply-To: "dc353@hotmail.com" <dc353@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "dc353@hotmail.com" <dc353@HOTMAIL.COM>
Organization: http://groups.google.com
Subject: Re: generate dates
Content-Type: text/plain; charset=UTF-8
On May 17, 11:24 pm, hs AT dc-sug DOT org ("Howard Schreier)" wrote:
> On Sat, 17 May 2008 19:47:06 -0700, dc...@hotmail.com <dc...@HOTMAIL.COM> wrote:
> >On May 17, 10:37 pm, "dc...@hotmail.com" <dc...@hotmail.com> wrote:
> >> On May 17, 10:16 pm, Andrew H Karp <sfbay0...@aol.com> wrote:
>
> >> > Here is a different approach for you to consider.
>
> >> > data dates;
> >> > do xdate = '01jan2003'd to '01apr2004'd by 28;
> >> > date = intnx('month',(xdate-1),1,'end');
> >> > output;
> >> > end;
> >> > drop xdate;
> >> > format date mmddyy10.;
> >> > run;
>
> >> > Hope this helps.
>
> >> > Andrew Karp
> >> > Sierra Information Serviceswww.sierrainformation.com
>
> >> > On May 17, 5:57�pm, "dc...@hotmail.com" <dc...@hotmail.com> wrote:
>
> >> > > On May 17, 8:29�pm, "dc...@hotmail.com" <dc...@hotmail.com> wrote:
>
> >> > > > Hi,
>
> >> > > > I'm trying to generate yyyymm dates in a do loop. �The following code
> >> > > > works but doesn't rely on SAS dates. �Is there an easier way using SAS
> >> > > > dates??
>
> >> > > > DATA _NULL_;
> >> > > > startdate = 200301;
> >> > > > enddate � = 200704;
>
> >> > > > yr = int(enddate/100) - int(startdate/100);
> >> > > > mt = (enddate - int(enddate/100)*100) - (startdate - int(startdate/
> >> > > > 100)*100);
> >> > > > if mt < 0 then do;
> >> > > > � �yr = yr - 1;
> >> > > > � �mt = mt + 12;
> >> > > > end;
>
> >> > > > cnt = yr*12 + mt;
> >> > > > syr = int(startdate/100);
> >> > > > smt = startdate - syr*100;
>
> >> > > > do i = 0 to cnt;
> >> > > > yr_inc = int((smt+i)/12);
> >> > > > mt_inc = mod(smt+i,12);
> >> > > > if mt_inc = 0 then mt_inc=12;
>
> >> > > > newdate= (syr+yr_inc)*100 + mt_inc;
> >> > > > put newdate;
> >> > > > end;
>
> >> > > > run;
>
> >> > > > What would be nice is something like:
>
> >> > > > do i = startdate to enddate (increment month by 1);
> >> > > > end;
>
> >> > > Ok, here's what I've been able to come up with: �I think this is
> >> > > what's going on: �We take a numeric value and PUT it into a character
> >> > > format, then when take the character value and INPUT it into a date
> >> > > format.
>
> >> > > DATA _NULL_;
> >> > > startdate = 200301;
> >> > > enddate � = 200404;
>
> >> > > startdate=input(put(startdate,6.),yymmn6.);
> >> > > enddate=input(put(enddate,6.),yymmn6.);
> >> > > cnt = intck('month',startdate,enddate);
> >> > > put cnt;
> >> > > do i = 0 to cnt;
> >> > > newdate = intnx('month',startdate,i,'end');
>
> >> > > format newdate yymmddn8.;
> >> > > put �newdate ;
>
> >> > > end;
>
> >> > > run;- Hide quoted text -
>
> >> > > - Show quoted text -
>
> >> thanks Andrew. Pretty compact.
>
> >I did a little more testing. if I have a longer difference between the
> >start and end date we get duplicate dates every once in awhile - this
> >is happening because of the BY 28. Using the INTCK function avoids
> >the problem.
>
> Try this variation:
>
> data dates;
> do until (date = '01apr2004'd);
> date = ifn( missing(date)
> , '01jan2003'd
> , intnx('month',date,1)
> );
> output;
> end;
> format date mmddyy10.;
> run;
Howard, that's pretty cool. do I read this as: if the date is
missing start with 01jan2003 and if the date is not missing intnx it
by 1 month? I've not seen the expression missing(date).
|