BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8

Hi,

I have a date variable which contains dates in this format

20111122

20120109

20111020

How can i change to meaningful SAS dates???

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Altal
Calcite | Level 5

then you can add a FORMAT to the above data step to show date however you want; for example:

data temp;

input mydate :yymmdd8.;

format mydate mmddyy10.;

cards;

20111122

20120109

20111020

;

run;

View solution in original post

9 REPLIES 9
AskoLötjönen
Quartz | Level 8

data temp;

input var1 $;

cards;

20111122

20120109

20111020

;

run;

data temp;

  set temp;

datevar=input(var1,yymmdd10.);

run;

robertrao
Quartz | Level 8

Hi,

I tried this and it gives me negative numbers!!!!

Could you help me find the error???

Thanks

data have;

input date;

cards;

20111122

20120109

20111020

;

run;


data want;

  set have;

datevar=input(date,yymmdd10.);

run;

Liz_LSU
Calcite | Level 5

Robert,

Are your dates before January 1, 1960? In SAS, day 0 is January 1, 1960, so any date earlier than that will be a negative number.

Use Altal's suggest to format your date variable as a date and you should see it printing as the correct date. Look at it without the format first, then with the format.

BTW, you can do this in one step, you don't have to use two! (I teach a SAS programming class and one of my pet peeves is using umpteen steps when one will do.)

liz

robertrao
Quartz | Level 8

Hi Liz,

Thanks for the reply. My dates are not prior 1960

I have those dates in a sas dataset and i am setting the dataset and creating several conditions and in the process i want to also change the format

It doesnot work for me!!!!

It stays the same even after i write

format var yymmdd10.

Thanks

Astounding
PROC Star

Your error is caused by two factors. 

1. Failing to read the documentation on the INPUT function, and thus failing to realize that it expects a character string as input not a numeric value.

2. Ignoring the message in the log about numeric to character conversion.

When the incoming value is numeric, the INPUT function performs the numeric-to-character conversion using a 12-character string.  Thus the INPUT function reads four blanks, followed by six digits.

You can correct the problem in two ways.  You could read DATE as character in the original INPUT statement.  Or, if you leave DATE as numeric, you could control the numeric-to-character conversion:

datevar = input(put(date,8.), yymmdd8.);

While you could stick with the yymmdd10. informat, it shows that you are paying attention when you use an 8-character informat (instead of a 10-character informat) to read 8 characters.

robertrao
Quartz | Level 8

Thanks Sir,

Its so worthful information to me.I appreciate your time and help

I noted the points you mentioned and where i went wrong

Now it works perfect except that the datevar contains days and we have to use another format to convert to the actual dates???

Thanks again

data want;

set have

datevar = input(put(date,8.), yymmdd8.);

format datevar yymmdd8.;

run;

Astounding
PROC Star

That's correct.  DATEVAR is stored on SAS's date scale where 0 = January 1, 1960.  The good news is that you can use any date format that you please to express DATEVAR.  Any of these (and more) would be possible:

format datevar yymmdd8.;

format datevar yymmdd10.;

format datevar mmddyy10.;

formate datevar date9.;

format datevar worddate18.;

format datevar monyy5.;

Good luck.

Liz_LSU
Calcite | Level 5

You can do this in one step by reading the data with an informat:

SASdateinformat.jpg

(You didn't say how your data actually look and how you specify an informat is a little different depending on whether you are using list input or column/formatted input.)

liz

Altal
Calcite | Level 5

then you can add a FORMAT to the above data step to show date however you want; for example:

data temp;

input mydate :yymmdd8.;

format mydate mmddyy10.;

cards;

20111122

20120109

20111020

;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1318 views
  • 3 likes
  • 5 in conversation