BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Dhana18
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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";

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

Must be your data isn't allowing this to work. Please show us (a portion of) your actual data set.

--
Paige Miller
Dhana18
Obsidian | Level 7
PATIENT_ID

Date

210

3-Jan-17

210

3-Jan-17

210

21-Feb-17

210

14-Mar-17

210

14-Mar-17

210

3-Apr-17

210

11-May-17

210

11-May-17

210

14-Jun-17

210

18-Jul-17

210

14-Aug-17

210

14-Aug-17

210

6-Sep-17

210

12-Oct-17

210

16-Nov-17

210

15-Dec-17

210

15-Dec-17


novinosrin
Tourmaline | Level 20

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;

Dhana18
Obsidian | Level 7
It is character.
PaigeMiller
Diamond | Level 26

You have to convert the dates to numeric for your code to work.

 

Something like

 

date_num = input(date,mmddyy8.);

 

 

--
Paige Miller
novinosrin
Tourmaline | Level 20

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";

Dhana18
Obsidian | Level 7
It worked! Thank you so much I really appreciate your help:)
Dhana18
Obsidian | Level 7

Thank you it worked!!!

Astounding
PROC Star

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;

novinosrin
Tourmaline | Level 20

Thank you sir. I will have to step out for starb for strong caffeine before i post more.  🙂

ballardw
Super User

You might consider using the YYQ. format with the date variable as well.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1159 views
  • 2 likes
  • 5 in conversation