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
Months | Quarters |
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 |
Thanks so much 🙂
/*
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*/
UNTESTED CODE
quarters=floor(mod(months,12)/3)+1;
@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;
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
@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.
/*
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*/
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.