- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 🙂
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
/*
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*/
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
UNTESTED CODE
quarters=floor(mod(months,12)/3)+1;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
/*
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*/