SAS: convert date to numeric - sas

I have a column that contains date values. So when imported as numeric, it shows 20668, 20669...etc. if I format it as yymmddn8, it shows 20160802 etc. However, what I really want is a numeric variable that shows 20160802. I have tried to create other to get day, month, year and then concatenate them. Unfortunately, the issue is if month and day is 1 digit, it would only show 201682. what would be the quickest way to achieve my goal. I guess a can turn the day and month variable to text and add 0 if day or month is less than 10. But this is not elegant and efficient. Please help.
Thanks

You can just wrap an input around that format:
data test;
date = 20668;
full_date = input(put(date,yymmddn8.),best12.);
run;
The put is converting the date to character in the format as you want it to appear, and the input with the best12. format is converting it back to numeric in that format.

It sounds like you just need to attach a format to your variable.
format date yymmddn8. ;
Try running this program to see a few of the different formats that are available for displaying dates.
data _null_;
do date = 20668, 20669 ;
put (6*date) (=10. =date9. =yymmddn8. =mmddyy10. =ddmmyy10. =yymmdd10.) ;
end;
run;

Related

convert a date from character to numeric

how can I convert a date (character) variable = 22249 (=30NOV2020) into 30NOV2020 (numeric)?
I tried many ways like input(), put() and so on but without success. Also setting date9., SAS output is: ERROR the format $date9 was not found or could not be loaded.
Many thanks in advance
Best
To convert a string like '22249' into a number like the date '30NOV2020'd just use the normal numeric informat. Once you have numeric variable with the value 22,249 you can attach any date type format to it to have it be displayed in a way that humans would recognize as a date.
data test;
string='22249';
date = input(string,32.);
format date date9.;
run;
Result
Obs string date
1 22249 30NOV2020

Convert Timestamp to Numeric value in SAS

How to convert the default timestamp "0001-01-01-00.00.00.000000" in SAS, i have tried below code but it has returned null value. Can someone help on this please
data _NULL_;
x = "0001-01-01-00.00.00.000000";
rlstime = input(x,anydtdtm26.);
call symput('rlstime',rlstime);
run;
%put rlst: &rlstime;
As far as I remember, SAS cannot do that. Any date/timestamp before 1.1.1600 doesn't exist for SAS. Do you need it or can you just replace it with a null value? If you really need it you could transform it into another valid timestamp, split it into different columns (year, month, etc.) or just use it as a string. In your example you just write the timestamp into the log, meaning it's not necessary to transform it.
The earliest date that SAS will handle is 1st January, 1582. Additionally, a colon character should be used to delimit the time from the date, as well as the hours, minutes and seconds. Therefore, your code may be adjusted to the following:
data _NULL_;
x = "1582-01-01:00:00:00.000000";
rlstime = input(x,anydtdtm26.);
call symput('rlstime',rlstime);
run;
%put rlst: &rlstime;

Excel to SAS Date not working in SAS

I Have a file from excel that is in a short date format, but when SAS reads it in, it turns it into numbers in the 4000 range...when I try and convert this to an excel date with the below formula, it turns the year into 2077...is there a formula to ensure that this date remains in the original format on the read in, or avoid it turning into this 4000 range that is not at all close to the 2017 and 2018 year that my file is starting in. Does that make sense?
data change_date;
format Completed_Date mmddyy8. ;
set check;
completed_date = date_completed;
if 42005 => date_completed >=43466 and date_completed ^=. then
Completed_date = Date_Completed-21916; *commented out 12-21-17 Xalka
dates back to how they are expected;
run;
I am pretty sure this is a duplicate question, but I can't find it.
This is usually caused by mixing character and date values in the same column. This made SAS import the data as a character variable and it results in the actual dates being copied as character versions of the integers that Excel uses to store dates.
Frequently this is caused by entries that look like dates but are really character strings in the Excel file. The best way to fix it is to fix the Excel file so that the column only contains dates. Otherwise you just need to convert the strings to integers and adjust the values to account for the differences in index dates.
So if your values are in a SAS dataset named HAVE in the character variable DATESTRING then you could use this data step to create a new variable with an actual date value.
data want ;
set have ;
if indexc(datestring,'-/') then date=input(datestring,anydtdte32.);
else date = input(datestring,32.) + '01JAN1900'D -2;
format date yymmdd10. ;
run;
The minus 2 is because of difference in whether to start numbering with 1 or 0 and because Excel thinks 1900 was a leap year.
Excel and SAS have different default dates in back-end.
Day 0 in SAS is 1 January 1960 and Day 0 in Excel is 1 January 1900.
So, you will need to convert excel numeric date to sas date using the below formula.
SAS_date = Excel_date - 21916;
data dateExample;
informat dt mmddyy8.;
set dates;
SAS_date = dates - 21916;
dt=sas_Date;
format dt date9.;
run;

SAS: convert date to Year Month Code

How do I convert 06JAN2005(as date format) to 200501 (as a number)
Note that there are only two different variable types in SAS - character and numeric. Dates are stored in numeric variables and are generally formatted to appear as user-readable dates. Specifically, dates are stored as the number of days since 01JAN1960.
So if you have a number already formatted as a date, and would like it to appear formatted differently (but still be a number) you simply need to change the format.
As Joe's comment says - I can't see the relationship between the date you provided and the number you desire (maybe you have a typo?). But the below piece of code will demonstrate how you can simply change the format to change the appearance of the number:
data _null_;
format date1 best.
date2 yymmddn6.
date3 date9.
;
date1 = '06JAN2005'd;
date2 = '06JAN2005'd;
date3 = '06JAN2005'd;
put date1= date2= date3=;
run;
Gives:
date1=16442 date2=050106 date3=06JAN2005
If you want an explicit numeric value, you can use the date functions on your date variable which, as pointed out, is a numeric date value SAS understands.
newdatevar=100*year(olddatevar)+month(olddatevar)
Well if you mean you need year||month then something like this should work:
data two;
set one;
order_month = month(date);
order_year = year(date);
order = put(compress(order_month||order_year),6.)-0;
run;
Disclaimer being I'd reccomend just formatting whatever else you have properly, so you don't need to represent the date as a number. If you're looking to add or substract increments of dates, you might want to look at the intx function too
Try this:
data a;
attrib
v_input format=date.
v_output_n format=8.;
v_input = '06JAN2005'd;
v_output_n = put(v_input,yymmn6.);
run;

How to convert String to Date value in SAS?

I want to convert a String to Date in SAS, I tried:
data _null_;
monyy = '05May2013';
date = input(substr(strip(monyy),1,9),yymmdd.);;
put date=date9.;
run;
But it did not work. Can this be done?
Formats like
date9.
or
mmddyy10.
are not valid for input command while converting text to a sas date. You can use
Date = input( cdate , ANYDTDTE11.);
or
Date = input( cdate , ANYDTDTE10.);
for conversion.
You don't need substr or strip.
input(monyy,date9.);
As stated above, the simple answer is:
date = input(monyy,date9.);
with the addition of:
put date=yymmdd.;
The reason why this works, and what you did doesn't, is because of a common misunderstanding in SAS. DATE9. is an INFORMAT. In an INPUT statement, it provides the SAS interpreter with a set of translation commands it can send to the compiler to turn your text into the right numbers, which will then look like a date once the right FORMAT is applied. FORMATs are just visible representations of numbers (or characters). So by using YYMMDD., you confused the INPUT function by handing it a FORMAT instead of an INFORMAT, and probably got a helpful error that said:
Invalid argument to INPUT function at line... etc...
Which told you absolutely nothing about what to do next.
In summary, to represent your character date as a YYMMDD. In SAS you need to:
change the INFORMAT - date = input(monyy,date9.);
apply the FORMAT - put date=YYMMDD10.;
Try
data _null_;
monyy = '05May2013';
date = input(substr(strip(monyy),1,9),date9.);
put date=date9.;
run;
input(char_val, date9.);
You can consider to convert it to word format using
input(char_val, worddate.)
You can get a lot in this page http://v8doc.sas.com/sashtml/lrcon/zenid-63.htm
This code helps:
data final; set final;
first_date = INPUT(compress(char_date),date9.); format first_date date9.;
run;
I personally have tried it on SAS
input(char_val,current_date_format);
You can specify any date format at display time, like set char_val=date9.;