Convert Timestamp to Numeric value in SAS - 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;

Related

SAS: convert date to numeric

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;

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;

Convert SAS numeric dates to datetime format

I am trying to convert a numeric date into date-time format. I am first subtracting a few days from the date I have.
$let from_date = "21JUL2016:00:00:00"dt;
data _null_;
datediff = intnx('dtday',&from_dt,-180);
call symput("cutoff_dt",datediff);
run;
After this I get numeric date like 17633333 which is fine because I am using this numeric date in pass through queries. But I also need to convert this date into datetime format like "21Dec2015:00:00:00"dt so that I can use this date in proc sql as well. So far after searching through sas documents and blogs I have been unable to do this. Help please.
You can use PUT to apply the format, but if you need quotes and the dt then that's a different story. If you do need them, I think that the current macro variable would work as well. Otherwise you have some other issue going on. There is no requirement for SQL to require the formatted value, unless you need a character or you're passing it through to a DB. In those cases the dt won't be required either.
%let from_date = "21JUL2016:00:00:00"dt;
data _null_;
datediff = intnx('dtday',&from_dt,-180);
call symput("cutoff_dt", put(datediff, datetime21.));
run;
SAS stores dates times and datetime values as numbers. Just like in a datastep
where you can write "where x='19feb2016'd" or "where x=20503" which is the number that SAS stores for the date=19feb2016, you can do the same in proc sql.
You only need to write the date value out as a character value if the variable you are comparing to contains the date as a character string. that is y has the value "19feb2016" and x has the value 20503, the test if y=x will not achieve what you want. That is when you have to write if y=put(x,date9.);

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.;

SAS date or numeric data?

%let months_back = %sysget(months_back);
data;
m = intnx('month', "&sysdate9"d, -&months_back - 2, 'begin');
m = intnx('day', put(m, date9.), 26, 'same');
m2back = put(m, yymmddd10.);
put m2back;
run;
NOTE: Character values have been converted to numeric values at the
places given by: (Line):(Column).
5:19 NOTE: Invalid numeric data, '01OCT2012' , at line 5 column 19.
I really don't know why this go wrong. The date string is numeric data?
PUT(m, date9.) is the culprit here. The 2nd argument of INTNX needs to be numeric (i.e. a date), the PUT function always returns a character value, in this instance '01OCT2012'. Just take out the PUT function completely and the code should work.
m = intnx('day', m, 26, 'same');
SAS stores dates as numbers - and in fact does not have a truly separate type for them. A SAS date is the number of days since 1/1/1960, so a bit over 19000 for today. The date format is entirely irrelevant to any date calculations - it is solely for human readibility.
The bit where you say:
"&sysdate9"d
actually converts the string "01JAN2012" to a numeric value (18304).
There's actually a quicker way to accomplish what you're trying to do. Because days correspond to whole numbers in SAS, to increment by one day you can simply add one to the value.
For example:
%let months_back=5;
data _null_;
m = intnx('month', today(), -&months_back - 2, 'begin');
m2 = intnx('day', m, 26, 'same');
m3 = intnx('month',"&sysdate9"d, -&months_back - 2)+26;
m2back = put(m2, yymmdd10.);
put m= date9. m2= yymmdd10. m3= yymmdd10.;
run;
M3 does your entire calculation in one step, by using the MONTH interval, then adding 26. INTNX('day'...) is basically pointless, unless there's some other value to using the function (using a shift index for example).
You also can see the use of a format in the PUT(log) statement here - you don't have to PUT it to a character value and then put that to the log to get the formatted value, just put (var) (format.); - and string together as many as you want that way.
Also, "&sysdate9."d is not the best way to get the current date. &sysdate. is only defined on startup of SAS, so if your session ran for 3 days you would not be on the current day (though perhaps that's desired?). Instead, the TODAY() function gets the current date, up to date no matter how long your SAS session has been running.
Finally - I recommend data _null_; if you don't want a dataset (and naming the result dataset if you do want it). data _null_ does not create a dataset. data; simply creates increasing numbers of datasets (data1, data2, ...) which quickly fill up your workspace and make it hard to tell what you're doing.