DATA Step, Macro, Functions and more

Convert Date9. to sas date

Reply
Occasional Contributor
Posts: 7

Convert Date9. to sas date

Hi , 

I am new to sas. I imported my data from excel  into sas and date is shwoing in this format ( 12Jul2014). I am trying to convert it into SAS format so I can calculate time intervals but it didnt work.  I used the following code 

 

Date New 

Set Old 

Newdate = input(olddate, date9.);

run;

The newdate variable have nothing but zeros .

 

any help will be appreciated 

 

Super User
Posts: 10,497

Re: Convert Date9. to sas date

First thing would be to check the status of the existing variable. Either us Proc Contents or properties of the variable by clicking on the column header when looking at the table. Check what the current assigned Format is. It is likely to be DATE9 already. If the Olddate variable is character check the spelling of the existing variable. If it isn't addressed yet then rerun the code and post the log including messages.

Occasional Contributor
Posts: 7

Re: Convert Date9. to sas date

[ Edited ]

Hi Ballardw; 

 

Thank you for your response. I have run the Proc contents , and the date variable is Numeric , with DATE9. format. Since its numeric, I was able to do subtraction and calculate the time intervals.

Thank you very much. 

Super User
Posts: 6,936

Re: Convert Date9. to sas date

To further elaborate on your initial scenario:

This code:

data old;
input olddate date9.;
format olddate date9.;
cards;
27sep2016
;
run;

data new;
set old;
newdate = input(olddate,date9.);
run;

will create the following log:

24         data new;
25         set old;
26         newdate = input(olddate,date9.);
27         run;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      26:17   
NOTE: Invalid argument to function INPUT at line 26 column 11.
olddate=27SEP2016 newdate=. _ERROR_=1 _N_=1
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to 
      missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 26:11   

The key part is the NOTE about the conversion, which comes from the use of a numeric variable in the input function; the input function is designed purely for inputting from character.

The failure to convert to a meaningful string that fits the date9 informat (implicit conversion is done with the best. format) then causes the Invalid Argument NOTE and the missing value.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 7

Re: Convert Date9. to sas date

KurtBremser, 

 

I went back through my log and I found exactly the same message

"Mathematical operations could not be performed at the following places. The results of the
operations have been set to missing values.

 

Using Proc Contents , I realized that the Date  variable is Numeric not Charcater .

Thank you for explaning it to me. It helped me understand the properties of the input function. 

Super User
Posts: 10,497

Re: Convert Date9. to sas date

[ Edited ]

If you are going to work with date type information very often you will want to investigate the functions INTNX and INTCK to increment dates or determine intervals as they are very flexible.

Also just changing the format can be used to group values in procedures.

For example look at the different results from the code below (uses your example data set OLD and date variable OLDDate)

proc freq data=old;
   tables  oldate;
run;

proc freq data=old;
   tables olddate;
   format olddate year4.;
run;

proc freq data=old;
   tables olddate;
   format olddate Month2.;
run;

Similar behavior can be used for report and most analyis procedures.

 

Ask a Question
Discussion stats
  • 5 replies
  • 1456 views
  • 3 likes
  • 3 in conversation