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

## How to construct earning string

Dear Madam/Sir,

I would like to construct quarterly earnings stream, but fail to locate related codes.

The data structure is below. How can I capture instances where 'qearning' is increased for four consecutive quarters (FQTR)?

Obs GVKEY FYEARQ FQTR qearning1234567891011121314
 001004 1994 3 0.12 001004 1994 4 0.147 001004 1995 1 0.133 001004 1995 2 0.153 001004 1995 3 0.173 001004 1995 4 0.207 001004 1996 1 0.2 001004 1996 2 0.213 001004 1996 3 0.24 001004 1996 4 0.26 001004 1997 1 0.267 001004 1997 2 0.307 001004 1997 3 0.34 001004 1997 4 0.38

Any programming advice will be highly appreciated.

Sincerely,

Joon

1 ACCEPTED SOLUTION

Accepted Solutions
Opal | Level 21

## Re: How to construct earning string

Something like below should work.

I've used your year and quarter column to derive a SAS Date value for the first day of the quarter. Using SAS Date values has many advantages like in below code to implement logic to deal with cases where you have gaps in your data (missing quarter).

Column consecutive_growth_cnt tells you in each row how many previous quarters had a lower earning.

``````data inter;
set have;
format reporting_dt yymon7.;
/* create column reporting_dt storing a SAS Date value first day of quarter */
/*  derived from year and quarter                                           */
reporting_dt=mdy(fqtr*3-2,1,fyearq);
run;

proc sort data=inter;
by gvkey reporting_dt;
run;

data want;
set inter;
by gvkey reporting_dt;

lag_qearning=lag(qearning);
lag_reporting_dt=lag(reporting_dt);

/* start of a new gvkey */
if first.gvkey then consecutive_growth_cnt=0;
/* for cases where there aren't consecutive quarters in the data */
else if intck('quarter',lag_reporting_dt,reporting_dt) ne 1 then consecutive_growth_cnt=0;
/* earning greater than previous quarter */
else if qearning>lag_qearning then consecutive_growth_cnt+1;
else consecutive_growth_cnt=0;

/*  format lag_reporting_dt yymon7.;*/
drop lag_qearning lag_reporting_dt;
run;``````

2 REPLIES 2
Opal | Level 21

## Re: How to construct earning string

Something like below should work.

I've used your year and quarter column to derive a SAS Date value for the first day of the quarter. Using SAS Date values has many advantages like in below code to implement logic to deal with cases where you have gaps in your data (missing quarter).

Column consecutive_growth_cnt tells you in each row how many previous quarters had a lower earning.

``````data inter;
set have;
format reporting_dt yymon7.;
/* create column reporting_dt storing a SAS Date value first day of quarter */
/*  derived from year and quarter                                           */
reporting_dt=mdy(fqtr*3-2,1,fyearq);
run;

proc sort data=inter;
by gvkey reporting_dt;
run;

data want;
set inter;
by gvkey reporting_dt;

lag_qearning=lag(qearning);
lag_reporting_dt=lag(reporting_dt);

/* start of a new gvkey */
if first.gvkey then consecutive_growth_cnt=0;
/* for cases where there aren't consecutive quarters in the data */
else if intck('quarter',lag_reporting_dt,reporting_dt) ne 1 then consecutive_growth_cnt=0;
/* earning greater than previous quarter */
else if qearning>lag_qearning then consecutive_growth_cnt+1;
else consecutive_growth_cnt=0;

/*  format lag_reporting_dt yymon7.;*/
drop lag_qearning lag_reporting_dt;
run;``````

Quartz | Level 8

## Re: How to construct earning string

Thank you so much, Patrick. Your code worked well. I greatly appreciate it. Have a good weekend.

Joon

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