I am working with a dataset in SAS, containing many variables.
One of these variables is a DATE variable and it has a date/time format. It looks like this:
12FEB97:00:00:00
27MAR97:00:00:00
14APR97:00:00:00
Now the thing is that I would like to convert this variable into a NUMERIC format. And I would like to get the following result (based on the previously shown 3 examples):
199702
199703
199704
Do you have any ideas how to do it? I already read through many docs, pdfs etc. but still could not find a proper solution yet.
Thank you very much!
Re: Convert SAS date to numericNumber_date= input(put(SAS_Date,DDMMYYn8.),8.);
conversion of partial date to numeric is not possible in SAS.
You use the PUT function to format a SAS date, time, or datetime value: PUT( sasDateOrTime, format.); The first argument to the PUT function is the SAS date, time, or datetime.
Specifically, SAS stores dates as numeric values equal to the number of days from January 1, 1960. That is, dates prior to January 1, 1960 are stored as unique negative integers, and dates after January 1, 1960 are stored as unique positive integers. So, for example, SAS stores: a 0 for January 1, 1960.
First, you need to extract the date from your datetime variable. You can use the datepart
function:
date = datepart(var);
Then, you want to put this variable (which will still be coded as a date number) into a number that you can read the year and month. Do this with putn
:
date_as_num = putn(date,'yymmn6.');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With