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

I am trying to create a month variable from days; I divided my days variable by 30.5. My data looks like this:

 

Days Month 
1       0.0327
1       0.0327
1       0.0327
1       0.0327
1       0.0327
2       0.065  
2       0.065
2       0.065
2       0.065
2       0.065
2       0.065
3       0.098
3       0.098
3       0.098

Is there an easier way to reclassify month other than 'if than else' statements? For example, I know I could do:

data temp;
set mydata;

if 0<=month<1 then month_new=1;
else if 1<=month<2 then month_new=2;
else if 2<=month<3 then month_new=3;
....
run;

But is there a more efficient way? For example, assuming there are thirteen months, I tried:

data temp;
retain month_new;
set mydata;

do i=1 to 13;
if i <= month < (i+1) then month_new=i;
end;

run;

This applied the last number in the do loop, 13, to all data values. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you are trying to bin your days into 30 day intervals (not really months since months do not have a constant number of days during a year or in case of Feb even across different years) just use some arithmetic.

month=floor(days/30);

If you want to count from 1 instead of 0 just add one.

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

In general, fractional months don't make a lot of sense. The better solution, if possible, is to handle months as SAS date variables and then use actual SAS date functions such as INTCK to determine number of months since a given date. Then all of this repetitive coding goes away. SAS has done the hard work in computing months so you don't have to.

--
Paige Miller
Tom
Super User Tom
Super User

If you are trying to bin your days into 30 day intervals (not really months since months do not have a constant number of days during a year or in case of Feb even across different years) just use some arithmetic.

month=floor(days/30);

If you want to count from 1 instead of 0 just add one.

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
  • 2 replies
  • 432 views
  • 0 likes
  • 3 in conversation