converting format (from date to numeric) using SAS - 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!

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

Related

Amazon Athena - Convert String to Date

I would like to convert some dates stored as a string to a date. The data looks as follows: 26APR2017:06:01:44. I would prefer it to be in any regular date format.
Can someone please help me to convert this to a date format? I'm sure the dateparse function is useful here and have used it before but am unsure how to adjust it for this particular data.
You can use date_parse() to parse and date() to cast timestamp retunred from parse function to date.
select date_parse('26APR2017:06:01:44','%d%b%Y:%H:%i:%s') will give you
2017-04-26 06:01:44.000
select date(date_parse('26APR2017:06:01:44','%d%b%Y:%H:%i:%s')) will give you
2017-04-26

Finding dates between two dates not working

I have a dataset where date_occur is in MMDDYY10 format (looks like 10/23/2014). I want all dates in FY17 which is 10/01/2016-09/30/2017. The following code is not working for some reason. Why!!
I'm not getting any error or warning messages I'm just getting an empty table.
Thanks in advance
data fy17;
set y16_17;
where date_occur between 10/01/2016 and 09/30/2017;
run;
The Log says:
NOTE: There were 0 observations read from the data set WORK.Y16_17.
WHERE (date_occur>=0.0001487357 and date_occur<=0.0049603175);
I'm thinking SAS is not understanding that date_occur is in mmddyy10 format. The internet has code like
where date_occur between '10/01/2016'd and '09/30/2017'd;
I tried this and it did not work either.
Your 'dates' are being interpreted as numeric expressions, and none of the date values in your table lie in the resulting range. Use date literals instead:
data fy17;
set y16_17;
where date_occur between '01oct2016'd and '30sep2017'd;
run;
You can only use date. format (ddmmmyyyy / ddmmmyy) when specifying date literals.

SAS Date Format conversion

I've recently started using SAS Enterprise guide and would like to convert the date "01MAR2014" to "MAR 2014". I've been googling but unfortunately no luck.
I've tried the following however it just returns an
SELECT input(put(StartDate,EURDFMYw.),9.) AS Order_DDD_YY
Does anyone know how to convert this?
If your date is a SAS date, i.e. a numeric with date format applied then you can change the display by using the monyy5. format instead. Note that this will create a character variable that will no longer sort properly in reports/datasets, it will sort alphabetically instead of a date order.
put(startDate, monyy7.) as Order_DDD_YY
Otherwise you can apply the format instead of converting it.

how to convert char date to sas date , tried many ways.

I have a data which is in one column which has a mix of all data types, formats.
text, numeric and also date.
I have to transpose the coloumn to rows.
When I import the data in to sas 9.3 the dates are turning into a number (char format) and after transpose when I try to convert using the input function it is not giving the correct date.
The observation I am getting is like 41172 in the date column.
How do I convert it?
Think you're confusing data types and formats. The two types are character and numeric.
Dates are all numeric, and are stored as a SASDATE which is the offset from 01 JAN 1960, with each day representing 1.
To have each SASDate be displayed, you must format that variable as a date (eg. date7, ddmmyy etc). That means you first need to use the input function to go from text to numeric (SASDate) then the put function to format it as you want it.
If you're using the input function, you just need to add one of those formats and a put function:
newvar=put(input(var,8.),date7.);
Or you could leave it as just the input() and use this:
format var date7.;
In your data step.

date format error while merging files using stata

I'm completely new to stata. I'm trying to merge 3 different datasets which have dates in them with format (d-mmm-yy). While trying to merge i'm encountering with an error saying
date is str 9 in using data stata
r(106)
I have no clue what this error is about. Need some help. I can provide any additional info if required.
Thanks
This probably means that in some data sets, the date is stored as a number (Stata's format is Unix-like, # of elapsed days since 1 Jan 1960), while in others, it is a string (which is exactly what Stata tells you). You need to convert them all to the same format, e.g. with
generate long n_date = date(date, "DMY", 2050)
See help date() or help date functions.