LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (May 2004, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Mon, 10 May 2004 15:33:08 -0400
Reply-To:   SAS Bigot <sas_bigot@ML1.NET>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   SAS Bigot <sas_bigot@ML1.NET>
Subject:   Re: Date format
In-Reply-To:   <D859A1A91D36184C8C28B77BF899C0860F6E0276@ladybird.tea.state.tx.us>
Content-Type:   text/plain; charset="US-ASCII"

When initially reading the data, why not input using sas date informat at the beginning.

data one; input aa yymmdd8. ; format aa yymmdd8. ; cards; 20031201 20040131 20041231 19610101 19020131 ; run;

Now variable aa will be a sas date from which you can easily perform date calculations. I added the date format, so if you view or print the data set, the integer will be seen in a meaningful format.

Instead of using a compress function to modify a common format, SAS provides another format that puts the date in the form you desire.

data four; set one; aaa = input ( put ( aa, yymmddn8. ), 8. ); prev_day = input ( put ( aa - 1, yymmddn8.), 8. ); next_day = input ( put ( aa + 1, yymmddn8.), 8. ); run;

A bigger question is why are you converting dates in this way. It's always best to keep dates as SAS dates. Use formats to affect how a SAS date is viewed or printed in a report. For example,

data four; set one; prev_day = aa - 1; next_day = aa + 1; run;

proc print; format aa prev_day next_day yymmddn8.; run;

You notice how much cleaner and easier to read your code becomes. If you wanted to apply the format to the variables permanently, then move the format statement from the proc print to the data step. Then the format becomes a permanent attribute of the variable. Something to think about.

----- Original message ----- From: "Dunn, Toby" <tdunn@TEA.STATE.TX.US> To: SAS-L@LISTSERV.UGA.EDU Date: Mon, 10 May 2004 11:06:16 -0500 Subject: Re: Date format

Try something like this: data one; input aa ; cards; 20031201 20040131 20041231 19610101 19020131 ;

data four(drop = aaa); set one; aaa=input(put(aa,8.),yymmdd10.); prev_day=compress(put(aaa-1,yymmdd10.),"-"); next_day=compress(put(aaa+1,yymmdd10.),"-"); run;

proc print data = four; run;

-----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Chen, Jian Sent: Monday, May 10, 2004 10:53 AM To: SAS-L@LISTSERV.UGA.EDU Subject: Date format

SAS-user:

The following codes that I wrote read in aa as a numeric. I wanted next day and previous day of aa, and output prev_day and next_day also as numeric. The codes work, but I think the codes in data four step can be shortened. Anyone has good ideas?

data one; input aa ; cards; 20031201 20040131 20041231 19610101 19020131 ;

data four(drop=_:); set one; _aaa=input(put(aa, 8.), yymmdd10.);

_prev_day=_aaa-1; _bb1=put(_prev_day, yymmdd10.); _prev_day_=compress(_bb1, '-'); prev_day=input(_prev_day_, 12.);

_next_day=_aaa+1; _bb2=put(_next_day, yymmdd10.); _next_day_=compress(_bb2, '-'); next_day=input(_next_day_, 12.); run;

Output:

Obs aa prev_day next_day

1 20031201 20031130 20031202 2 20040131 20040130 20040201 3 20041231 20041230 20050101 4 19610101 19601231 19610102 5 19020131 19020130 19020201

-- http://www.fastmail.fm - mmm... Fastmail...


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