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

I am trying to group my data into the following data ranges, it's not working and I am still learning my way around SAS. Any thoughts?

 

data=contact_date_interval;
set work.contact_ind;
format EFFECTIVE_FROM date9.;
date=datepart(EFFECTIVE_FROM);
if date >= '01NOV2018'd and date <= '28Feb2019'D then
Group ="1Nov2018 - 28Feb2019";
else if date >='01MAR2019'D and date <='06MAY2019'D then
Group = "1March - 6May2019";
else if date >='07MAY2019'D and date <='31MAY2019'D then
Group = "7May - 31May2019";
else if date >='01JUN2019'D and date <='31JUL2019'D then
Group = "1Jun - 31Jul2019";
else Group ='Other';
run;

 

I would also like to view the results as a count. 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

1. Remove the = sign on the first line

2. The code seems fine. What error do you get?

3. This is easier to read:

data CONTACT_DATE_INTERVAL;
  set WORK.CONTACT_IND;
  format EFFECTIVE_FROM date9.;
  DATE=datepart(EFFECTIVE_FROM);
  if      '01NOV2018'd <= DATE <= '28FEB2019'd then GROUP = "1Nov2018 - 28Feb2019";
  else if '01MAR2019'd <= DATE <= '06MAY2019'd then GROUP = "1March - 6May2019";
  else if '07MAY2019'd <= DATE <= '31MAY2019'd then GROUP = "7May - 31May2019";
  else if '01JUN2019'd <= DATE <= '31JUL2019'd then GROUP = "1Jun - 31Jul2019";
  else                                              GROUP = 'Other';
run;

 

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

1. Remove the = sign on the first line

2. The code seems fine. What error do you get?

3. This is easier to read:

data CONTACT_DATE_INTERVAL;
  set WORK.CONTACT_IND;
  format EFFECTIVE_FROM date9.;
  DATE=datepart(EFFECTIVE_FROM);
  if      '01NOV2018'd <= DATE <= '28FEB2019'd then GROUP = "1Nov2018 - 28Feb2019";
  else if '01MAR2019'd <= DATE <= '06MAY2019'd then GROUP = "1March - 6May2019";
  else if '07MAY2019'd <= DATE <= '31MAY2019'd then GROUP = "7May - 31May2019";
  else if '01JUN2019'd <= DATE <= '31JUL2019'd then GROUP = "1Jun - 31Jul2019";
  else                                              GROUP = 'Other';
run;

 

RALL
Obsidian | Level 7

It ran when I used yours, I am assuming it was the = in the beginning or the order. If I wanted to have a table of the counts in those date ranges what would the code for that look like?

ballardw
Super User

You may want to consider a custom format such as:

proc format library=work;
value dategroup
'01NOV2018'd - '28FEB2019'd = "1Nov2018 - 28Feb2019"
'01MAR2019'd - '06MAY2019'd = "1March - 6May2019"
'07MAY2019'd - '31MAY2019'd = "7May - 31May2019"
'01JUN2019'd - '31JUL2019'd = "1Jun - 31Jul2019"
other= 'Other';
run;

proc freq data=work.contact_date_interval;
   tables date;
   format date dategroup.;
run;

Without explicitly creating a group variable and using the format associated with the date if you have to add another interval late such as 1Aug2019- 4Sep2019. Then by changing the format the group will change with the format.

 

Categories created with formats will be used by most analysis, reporting and graphing procedures.

 

And actually a format could be made using your raw datetime values as well..

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2390 views
  • 1 like
  • 3 in conversation