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

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
001004199430.120
001004199440.147
001004199510.133
001004199520.153
001004199530.173
001004199540.207
001004199610.200
001004199620.213
001004199630.240
001004199640.260
001004199710.267
001004199720.307
001004199730.340
001004199740.380

 

Any programming advice will be highly appreciated.

 

Sincerely,

Joon

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

Patrick_0-1635556164013.png

 

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

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;

Patrick_0-1635556164013.png

 

joon1
Quartz | Level 8

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

Joon

sas-innovate-2024.png

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.

 

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
  • 2 replies
  • 332 views
  • 0 likes
  • 2 in conversation