Why isn't this working"
data surrg.merged_date_cat;
set surrg.merged_date;
if date >= '01JAN2017'D and date <='31JAN2017'D then date_cat="Jan_mar";
if date >= '01APR2017'D and date <='01JUN2017'D then date_cat="Apr_Jun";
if date >= '01JUN2017'D and date <='30SEP2017'D then date_cat="Jul_Sep";
if date >= '09OCT2017'D and date <='31DEC2017'D then date_cat="Oct_Dec";
run;
Oh well in that case
try
if '01JAN2017'D<= input(date,date9.)<='31mar2017'D then date_cat="Jan_mar";
instead of
if date >= '01JAN2017'D and date <='31JAN2017'D then date_cat="Jan_mar";
Must be your data isn't allowing this to work. Please show us (a portion of) your actual data set.
Not sure. Can you post your log and few details, i just see one change '01JUL2017'D
Is you date numeric or char variable?
data surrg.merged_date_cat;
set surrg.merged_date;
if date >= '01JAN2017'D and date <='31JAN2017'D then date_cat="Jan_mar";
if date >= '01APR2017'D and date <='01JUN2017'D then date_cat="Apr_Jun";
if date >= '01JUL2017'D and date <='30SEP2017'D then date_cat="Jul_Sep";
if date >= '09OCT2017'D and date <='31DEC2017'D then date_cat="Oct_Dec";
run;
You have to convert the dates to numeric for your code to work.
Something like
date_num = input(date,mmddyy8.);
Oh well in that case
try
if '01JAN2017'D<= input(date,date9.)<='31mar2017'D then date_cat="Jan_mar";
instead of
if date >= '01JAN2017'D and date <='31JAN2017'D then date_cat="Jan_mar";
Thank you it worked!!!
I can see three more:
data surrg.merged_date_cat;
set surrg.merged_date;
if date >= '01JAN2017'D and date <='31MAR2017'D then date_cat="Jan_mar";
if date >= '01APR2017'D and date <='30JUN2017'D then date_cat="Apr_Jun";
if date >= '01JUL2017'D and date <='30SEP2017'D then date_cat="Jul_Sep";
if date >= '01OCT2017'D and date <='31DEC2017'D then date_cat="Oct_Dec";
run;
For safety, I would change the structure of the statements.
data surrg.merged_date_cat;
set surrg.merged_date;
cutoff = date; /* if date is actually numeric */
cutoff = input(date, date9.); /* if date is actually character */
if cutoff >= '01JAN2018'D then return;
else if cutoff >= '01OCT2017'D then date_cat="Oct_Dec";
else if cutofff >= '01JUL2017'D then date_cat="Jul_Sep";
else if cutoff >= '01APR2017'D then date_cat="Apr_Jun";
else if cutoff >= '01JAN2017'D then date_cat="Jan_mar";
run;
run;
Thank you sir. I will have to step out for starb for strong caffeine before i post more. 🙂
You might consider using the YYQ. format with the date variable as well.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.