0
votes

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!

2
That should say 'OrderDate' column not 'Orders'Patrick
SAS stores all dates as numbers and simply formats them at the output. But you may get all you need by changing the format to year using the format YEARw. with the format function.JJFord3
Hi there, how would I use that? Have looked online but I don't really understand it. Thanks.Patrick

2 Answers

0
votes

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;
0
votes

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;