Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting format (from date to numeric) using SAS

Tags:

sas

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!

like image 249
Laszlo Avatar asked Oct 12 '11 14:10

Laszlo


People also ask

How do I convert a date to a number in SAS?

Re: Convert SAS date to numericNumber_date= input(put(SAS_Date,DDMMYYn8.),8.);

How do I convert partial dates to numeric in SAS?

conversion of partial date to numeric is not possible in SAS.

How do I change the date format 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.

How does SAS calculate the numeric value of a date?

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.


1 Answers

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.');
like image 98
itzy Avatar answered Sep 22 '22 07:09

itzy