Help using Base SAS procedures

SAS DATES

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,041
Accepted Solution

SAS DATES

Hi,

I have a date variable which contains dates in this format

20111122

20120109

20111020

How can i change to meaningful SAS dates???

Thanks


Accepted Solutions
Solution
‎08-29-2013 12:02 PM
Contributor
Posts: 62

Re: SAS DATES

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


All Replies
Contributor
Posts: 44

Re: SAS DATES

Posted in reply to robertrao

data temp;

input var1 $;

cards;

20111122

20120109

20111020

;

run;

data temp;

  set temp;

datevar=input(var1,yymmdd10.);

run;

Super Contributor
Posts: 1,041

Re: SAS DATES

Posted in reply to AskoLötjönen

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;

Occasional Contributor
Posts: 9

Re: SAS DATES

Posted in reply to robertrao

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

Super Contributor
Posts: 1,041

Re: SAS DATES

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

Super User
Posts: 5,503

Re: SAS DATES

Posted in reply to robertrao

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.

Super Contributor
Posts: 1,041

Re: SAS DATES

Posted in reply to Astounding

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;

Super User
Posts: 5,503

Re: SAS DATES

Posted in reply to robertrao

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.

Occasional Contributor
Posts: 9

Re: SAS DATES

Posted in reply to robertrao

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

Solution
‎08-29-2013 12:02 PM
Contributor
Posts: 62

Re: SAS DATES

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;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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