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

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  🙂

1 ACCEPTED SOLUTION

Accepted Solutions
ShiroAmada
Lapis Lazuli | Level 10
/* 
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

5 REPLIES 5
PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

quarters=floor(mod(months,12)/3)+1;
--
Paige Miller
novinosrin
Tourmaline | Level 20

@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;

 

 

 

 

ErikLund_Jensen
Rhodochrosite | Level 12

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

 

 

PaigeMiller
Diamond | Level 26

@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
ShiroAmada
Lapis Lazuli | Level 10
/* 
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*/

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 5 replies
  • 5304 views
  • 0 likes
  • 5 in conversation