Help using Base SAS procedures

how to convert character date to yymmdd8

Reply
Contributor
Posts: 74

how to convert character date to yymmdd8

Hi,
my input file has the following data:

date$
20071206
20011102

20071009
i use the following:
dt=input(date,yymmdd8.);
but my output looks like

07-12-06
01-11-02
.
07-10-09
because of the "." i can getting error invlaid argument used in input statement.

can anybody tell me how can i do this.

thanks
Super Contributor
Super Contributor
Posts: 3,174

Re: how to convert character date to yymmdd8

You will want to use a SAS FORMAT statement or an ATTRIB stmt FMT= parameter to define a desirable SAS numeric DATE format to suit your needs. You must have a format being associated by default.

Scott Barry
SBBWorks, Inc.
Super User
Posts: 9,662

Re: how to convert character date to yymmdd8

Hi. I think 'yymmddn8' should be useful.
[pre]
dt=input(date,yymmdd8.);
format dt yymmddn8.;
put dt=;
[/pre]


Ksharp
SAS Super FREQ
Posts: 8,740

Re: how to convert character date to yymmdd8

Hi:
In this instance, it would be useful to see your WHOLE program log and messages. I doubt that the '.' is what's generating the error message. I can use your same data in a program and yyddmm8. and do not get an error message.

There is a difference between the INFORMAT that you use to read in a date value and the FORMAT that you use to display a date value. The yymmdd8. is the correct format for reading in the date, since there are no delimiters in the date, it is translated from a character value to a numeric number of days since Jan 1, 1960.

The display of your date field is different however, yymmdd FORMAT (versus INFORMAT) wants there to be separators in a DISPLAYED date. so a FORMAT of yymmdd8. will result in the display you showed.

Here's the program I ran and the results are at the very bottom of the mail. Note the difference between the different PUT statements in the log. Each PUT statement uses a different FORMAT -- with the first PUT statement showing the internal value of the variables. Compare the results of the YYMMDD8. format with the YYMMDDN8. format -- the 'N' in the format name tells SAS not to use any separators for the date when the date is DISPLAYED.
[pre]
data chardate;
infile datalines;
input date $;
newdate = input(date,yymmdd8.);
put '***************************************';
put 'newdate is (internal value): ' newdate;
put 'newdate formatted 1: ' newdate date7.;
put 'newdate formatted 2: ' newdate yymmdd8.;
put 'newdate formatted 3: ' newdate yymmdd10.;
put 'newdate formatted 4: ' newdate yymmddn8.;
put 'newdate formatted 5: ' newdate worddate.;
return;
datalines;
20071206
20011102
;
run;
[/pre]

cynthia

The entire SAS log:
[pre]
296 data chardate;
297 infile datalines;
298 input date $;
299 newdate = input(date,yymmdd8.);
300 put '***************************************';
301 put 'newdate is (internal value): ' newdate;
302 put 'newdate formatted 1: ' newdate date7.;
303 put 'newdate formatted 2: ' newdate yymmdd8.;
304 put 'newdate formatted 3: ' newdate yymmdd10.;
305 put 'newdate formatted 4: ' newdate yymmddn8.;
306 put 'newdate formatted 5: ' newdate worddate.;
307 return;
308 datalines;

***************************************
newdate is (internal value): 17506
newdate formatted 1: 06DEC07
newdate formatted 2: 07-12-06
newdate formatted 3: 2007-12-06
newdate formatted 4: 20071206
newdate formatted 5: December 6, 2007
***************************************
newdate is (internal value): 15281
newdate formatted 1: 02NOV01
newdate formatted 2: 01-11-02
newdate formatted 3: 2001-11-02
newdate formatted 4: 20011102
newdate formatted 5: November 2, 2001
NOTE: The data set WORK.CHARDATE has 2 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.43 seconds
cpu time 0.01 seconds


311 ;
312 run;

[/pre]
Ask a Question
Discussion stats
  • 3 replies
  • 5702 views
  • 0 likes
  • 4 in conversation