How to convert date field to this format 2018 / 01 - date

I have one date field in my table as char "01jan2017",
I want to convert the date field to this format "2018 / 01" and there should be space between forward slash.
Thanks

If what you're looking for is just for display then here is a character conversion
data r;
date = '01jan2017'd;
date1 = compbl(put(year(date),best.)|| " / "||put(month(date),z2.));
run;

There are three key steps you need to do:
catx(' / ',year(input(date_char,date9.)),put(month(input(date_char,date9.)),z2.));
Convert the date to date9. format in order to extract the year and month,
Use the z2. format for the month to get the leading Zero,
Use Catx() to concatinate the year , month & ' / '.
Full Code:
data want;
date_char="01jan2017";
dateYYMM=catx(' / ',year(input(date_char,date9.)),put(month(input(date_char,date9.)),z2.));
run;
Output:
date_char=01jan2017 dateYYMM=2017 / 01

Related

Sas changing of date format

I have three columns with date formatted differently in SAS:
12 june 2017 00:15 - full date
2016 - only year
12 - only month
I Need to change the format of date and subtract after the dates to get results in the number of months.
for instance, "12 June 2017 00:15" - December 2016 = 7
how to do it?
As you have probably already found, there isn't a ready-made SAS date informat that will correctly handle your full date field, so you'll need to write a bit of custom logic to convert it before doing your calculation. date9. is the closest matching format I could find:
data example;
fulldate = '12 june 2017 00:15';
year = 2016;
month = 12;
/* Convert string to date9 format and input */
fulldate_num = input(
cats(
scan(fulldate,1),
substr(scan(fulldate,2,' '),1,3),
scan(fulldate,3)
), date9.
);
/* Calculate difference in months */
monthdiff = intck('month', mdy(month,1,year), fulldate_num);
run;
Convert the "full date" field to a SAS date value.
Convert the combo of year and month to a SAS date value, too.
Use the INTCK function to find the difference in months.
For example:
data dates ;
input dt $18. yy mm ;
mm_diff = intck ("mon", input (cats (yy, mm), yymmn6.), input (dt, anydtdte12.)) ;
put mm_diff= ;
cards ;
12 june 2017 00:15 2016 12
11 june 2018 00:15 2017 3
;
run ;
The log will print:
mm_diff=6
mm_diff=15
As a side note, the statement "there isn't a ready-made SAS date informat that will correctly handle your full date field" made elsewhere in this thread is incorrect. As the program snippet above shows, the ANYDTDTEw. informat handles it with aplomb. It's just incumbent upon the programmer to supply a sufficient informat width W. Above, it is selected as W=12. If you're reluctant to guess and/or count, just use ANYDTDTE32.
Regards,
Paul Dorfman
Assuming that you have three numeric variables and the first one contains valid SAS datetime values you should first convert both to valid SAS date values. You can then use the INTCK() function to count months.
nmonths = intck('month',datepart(VAR1),mdy(VAR3,1,VAR2));

teradata yymmdd date format

I have a field that should be 6 digit character but it is numeric. I am using the following code to add the leading zero:
select CAST(CAST(CHD_OPEN_DATE AS FORMAT '9(6)') AS CHAR(9))
I'm using the following code to format this as a date:
cast(cast(lpad(to_char(CHD_OPEN_DATE),6,'0') as date format 'YYMMDD') as date format 'YYYY-MM-DD')
When using this date format 1990 comes up as 2090. Is there a work-around for this?
If your number has a YYMMDD format you can use the following to cast to a date without the need to cast to an intermediate string. Assuming a date range between 1930 and 2029:
SELECT 900331 AS CHD_OPEN_DATE,
Cast(CASE WHEN CHD_OPEN_DATE < 300000
THEN CHD_OPEN_DATE + 1000000
ELSE CHD_OPEN_DATE
END AS DATE)

Changing the values of Dates in SAS

Complete novice with SAS and I'm trying to convert a yearly range of dates to just "2014", "2015" & "2016." So for example I have an Orders column with a lots of dates in 2014, 2015 and 2016 and want to just convert the values in each year to just the name of the year. The code I was trying to use is below.
Data SortingDates;
set work.ClaraData;
if OrderDate <='31Dec2014'd then OrderDate = "2014";
if '01Jan2015'd <= OrderDate <= '31Dec2015'd then OrderDate= "2015";
if '01Jan2016'd <= OrderDate <= '31Dec2016'd then OrderDate = "2016";
run;
However this message comes: Character values have been converted to numeric values at the places given by...
Plus when printing the data, the dates all come out as 09/07/1965
The OrderDate column is properly formatted as "OrderDate Num 8 DDMMYY10. DDMMYY10."
Thanks!
You are getting the warning because you tried to assign the characters string "2014" to the numeric variable OrderDate. SAS probably successfully converted "2014" into 2,014 for you but since you didn't change the format it should display it as '07/07/1965' since that is the date that is 2,014 days since 01JAN1960.
It is probably easiest if you use the YEAR() function to get the year of a date value.
OrderYear = year(OrderDate);
But you could also just try using the YEAR. format on your existing OrderDate variable.
proc freq data=ClaraData ;
tables OrderDate ;
format OrderDate year. ;
run;
Try the year function (page 15 of this PDF): https://www.sas.com/storefront/aux/en/spfunctionxexample/62857_excerpt.pdf
Data SortingDates;
set work.ClaraData;
OrderDate = YEAR(OrderDate);
run;
Or keeping it as a date, try the year format (like page 8 of the same pdf)
Data SortingDates;
set work.ClaraData;
format OrderDate YEARw.;
run;

convert numeric date into DATE in SAS Enterprise Guide

I have a date column which dates are showing as numeric, such as "201101", "201203"...
How can I convert these numeric date into format as "Jan2011", "Mar2012" by using SAS enterprise?
Thanks!
You can do it like this:
DATA test;
input date;
CARDS;
201101
201203
;
RUN;
data test2;
set test;
date2 = input(put(date,6.),yymmn6.);
format date2 monyy7.;
run;
Marti Mayo's answer is correct - but just wanted to point out that it only needs the one step:
DATA test;
format date monyy7. ; *<--- Sets the format you want to view (JAN2011) ;
input date :yymmn6.; *<--- Sets the informat of what you read in(201101);
CARDS;
201101
201203
;RUN;

SAS - custom date format

I'm trying to read in a date field that looks like:
Mar 20 2013 12:00AM
I am using the following user-defined date format and it is not working.
proc format;
picture mydate other='%MON %0d %Y %0H:%0M %p' (datatype=datetime);
run;
data DATASET;
infile CSVFILE
delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
informat TestDate mydate. ;
format TestDate mydate. ;
run;
Can anyone spot what is wrong with this? This is the first time I've needed to use a custom date format and I'm thinking I am missing something small. I am getting the following error:
NOTE: Informat MYDATE was not found or could not be loaded.
I do not believe you can create INformats using Picture; only formats. (INformat = taking a string and converting to a (in this case) date value, format = taking a date value and converting to a string.)
Fortunately, ANYDTDTM. seems to read this in fine. (I changed to 11AM to make sure the time part was okay.)
data test;
input #1 x ANYDTDTM19.;
put x= DATETIME17.;
datalines;
Mar 20 2013 11:00AM
;;;;
run;