DATA Step, Macro, Functions and more

How to change Months into Quarters

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

How to change Months into Quarters

Hi all, I have series of months numbers and I want to change it into quarters number, but I don't have the data in date format so I can't use quarter() statement.

Like following: what I want is the Quarters column

MonthsQuarters
01
11
21
32
42
52
63
73
83
94
104
114
121
131
141
152
162
172
183
193
203

 

Thanks so much  Smiley Happy


Accepted Solutions
Solution
‎09-18-2017 04:58 PM
Frequent Contributor
Posts: 100

Re: How to change Months into Quarters

/* 
Try this one. 
#1. Build lookup table
*/


data HAVE;
  do months= 0 to 71;
    output;
  end;
run;

/*#2 define quater */

data WANT;
  set HAVE;
  TAG+1;
  if TAG=13 then TAG=1;
    else if 4<=TAG <=6 then QUARTERS=2;
    else if 7<=TAG <=9 then QUARTERS=3;
    else if 10<=TAG <=12 then QUARTERS=4;

retain TAG;
run;

/*
#3 I intentionally left the variable TAG for you to see that no matter what the value of MONTHS tag will be
between 1 and 12

data set WANT may serve as your lookup table and you can use it to merge or join with other 
tables whenever you need to derive the quarter 


Here's how: 
*/

data ACTUAL;
  input months 8.;
cards;
8
4
12
17
55
1
3
7
;
run;
proc sql;
  create table actual_with_qtr as
  select a.*, b.quarters from ACTUAL a left join WANT b
  on a.months=b.months;
quit;

/*Hope this helps*/

View solution in original post


All Replies
Trusted Advisor
Posts: 1,932

Re: How to change Months into Quarters

UNTESTED CODE

 

quarters=floor(mod(months,12)/3)+1;
PROC Star
Posts: 283

Re: How to change Months into Quarters

[ Edited ]
Posted in reply to PaigeMiller

@Linmuxi  if i understood you-

 

data have(drop=Quarters);

input Months    Quarters;

datalines;

0    1

1    1

2    1

3    2

4    2

5    2

6    3

7    3

8    3

9    4

10   4

11   4

12   1

13   1

14   1

15   2

16   2

17   2

18   3

19   3

20   3

;

 

 

data want;

do _n_=1 by 1 until(_n_=12);

set have end=last;

if _n_=1 then quarters=1;

output;

if mod(_n_,3)=0 then quarters+1;

end;

run;

 

 

 

 

Contributor
Posts: 34

Re: How to change Months into Quarters

hi linmuxi

 

In this case uou could use the QTR function by translating the month to a date by using the MDY funtion with constants as values for day and year. Any year and any day (less than 29) will give the same result:

 

230 data _null_;
231 do months = 1 to 12;
232 quarters = qtr(mdy(months,1,2017));
233 put months= quarters=;
234 end;
235 run;

months=1 quarters=1
months=2 quarters=1
months=3 quarters=1
months=4 quarters=2
months=5 quarters=2
months=6 quarters=2
months=7 quarters=3
months=8 quarters=3
months=9 quarters=3
months=10 quarters=4
months=11 quarters=4
months=12 quarters=4

 

 

Trusted Advisor
Posts: 1,932

Re: How to change Months into Quarters

Posted in reply to ErikLund_Jensen

ErikLund_Jensen wrote:

hi linmuxi

 

In this case uou could use the QTR function by translating the month to a date by using the MDY funtion with constants as values for day and year. Any year and any day (less than 29) will give the same result:

 

230 data _null_;
231 do months = 1 to 12;
232 quarters = qtr(mdy(months,1,2017));
233 put months= quarters=;
234 end;
235 run;

months=1 quarters=1
months=2 quarters=1
months=3 quarters=1
months=4 quarters=2
months=5 quarters=2
months=6 quarters=2
months=7 quarters=3
months=8 quarters=3
months=9 quarters=3
months=10 quarters=4
months=11 quarters=4
months=12 quarters=4

 

 


But the original request had months up to 20; and furthermore month 0 is in quarter 1, month 3 is in quarter 2, month 12 is in quarter 1, etc. Your answer doesn't handle this.

Solution
‎09-18-2017 04:58 PM
Frequent Contributor
Posts: 100

Re: How to change Months into Quarters

/* 
Try this one. 
#1. Build lookup table
*/


data HAVE;
  do months= 0 to 71;
    output;
  end;
run;

/*#2 define quater */

data WANT;
  set HAVE;
  TAG+1;
  if TAG=13 then TAG=1;
    else if 4<=TAG <=6 then QUARTERS=2;
    else if 7<=TAG <=9 then QUARTERS=3;
    else if 10<=TAG <=12 then QUARTERS=4;

retain TAG;
run;

/*
#3 I intentionally left the variable TAG for you to see that no matter what the value of MONTHS tag will be
between 1 and 12

data set WANT may serve as your lookup table and you can use it to merge or join with other 
tables whenever you need to derive the quarter 


Here's how: 
*/

data ACTUAL;
  input months 8.;
cards;
8
4
12
17
55
1
3
7
;
run;
proc sql;
  create table actual_with_qtr as
  select a.*, b.quarters from ACTUAL a left join WANT b
  on a.months=b.months;
quit;

/*Hope this helps*/
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 139 views
  • 0 likes
  • 5 in conversation