assign quarter numbers to different dates

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

assign quarter numbers to different dates

Hi, I have a question, which I think it's not difficult. But I just don't know how to do.

I have a dataset including two date variables, start date and end date The date variable is for quarter, and is not continuous. Now I want to create two quarter variables corresponding to two dates. Look at the example below

stdate               enddate               stquarter          endquarter

1980.1               1980.4                    1                    4

1980.2               1981.1                    2                    5

1981.1               1981.2                    5                    6

1981.3               1981.4                    7                    8

...

1980.1 means the first quarter of 1980, and 1981.3 means the third quarter of 1981. I want to create the column "quarter". I treat 1980.1 as the first observed quarter, and others follow. If the date is continuous I know how to do.

I learned from here how to do for one date variable, but for two I have some difficulty. Thanks in advance.


Accepted Solutions
Solution
‎05-20-2014 08:45 PM
Respected Advisor
Posts: 3,124

Re: assign quarter numbers to different dates

My educated guess is that both of your date came in as Char, if not, it is even simpler. So here it goes:

data have;

input (stdate   enddate) (:$);

_s=yyq(scan(stdate,1),scan(stdate,2));

_e=yyq(scan(enddate,1),scan(enddate,2));

retain _i;

if _n_=1 then _i=_s;

stqtr=intck('qtr',_i,_s)+1;

endqtr=intck('qtr',_i,_e)+1;

drop _:;

cards;

  1. 1980.1               1980.4                 
  2. 1980.2               1981.1                 
  3. 1981.1               1981.2                 
  4. 1981.3               1981.4                 

;


Regards,

Haikuo

View solution in original post


All Replies
Solution
‎05-20-2014 08:45 PM
Respected Advisor
Posts: 3,124

Re: assign quarter numbers to different dates

My educated guess is that both of your date came in as Char, if not, it is even simpler. So here it goes:

data have;

input (stdate   enddate) (:$);

_s=yyq(scan(stdate,1),scan(stdate,2));

_e=yyq(scan(enddate,1),scan(enddate,2));

retain _i;

if _n_=1 then _i=_s;

stqtr=intck('qtr',_i,_s)+1;

endqtr=intck('qtr',_i,_e)+1;

drop _:;

cards;

  1. 1980.1               1980.4                 
  2. 1980.2               1981.1                 
  3. 1981.1               1981.2                 
  4. 1981.3               1981.4                 

;


Regards,

Haikuo

Frequent Contributor
Posts: 122

Re: assign quarter numbers to different dates

Great! Thanks.

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 187 views
  • 0 likes
  • 2 in conversation