09-27-2016 06:39 PM
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
Newdate = input(olddate, date9.);
The newdate variable have nothing but zeros .
any help will be appreciated
09-27-2016 07:07 PM
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.
09-27-2016 10:19 PM - edited 09-27-2016 10:22 PM
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.
09-28-2016 02:27 AM
To further elaborate on your initial scenario:
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.
09-28-2016 03:58 PM
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.
09-28-2016 10:58 AM - edited 09-28-2016 06:47 PM
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.