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)?
001004 | 1994 | 3 | 0.120 |
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.200 |
001004 | 1996 | 2 | 0.213 |
001004 | 1996 | 3 | 0.240 |
001004 | 1996 | 4 | 0.260 |
001004 | 1997 | 1 | 0.267 |
001004 | 1997 | 2 | 0.307 |
001004 | 1997 | 3 | 0.340 |
001004 | 1997 | 4 | 0.380 |
Any programming advice will be highly appreciated.
Sincerely,
Joon
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;
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;
Thank you so much, Patrick. Your code worked well. I greatly appreciate it. Have a good weekend.
Joon
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.