BookmarkSubscribeRSS Feed
Jordani
Obsidian | Level 7

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 

 

5 REPLIES 5
ballardw
Super User

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.

Jordani
Obsidian | Level 7

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. 

Kurt_Bremser
Super User

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.

Jordani
Obsidian | Level 7

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. 

ballardw
Super User

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.

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 11113 views
  • 4 likes
  • 3 in conversation