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 tried to compute differences between quarters in the same company using the following code, but it did not work.

GVKEYDATADATEFYEARQFQTRsaley

 

1004198508311985153.836
1004198511301985261.04
1004198602281985364.541
1004198605311985468.595
1004198608311986166.376
1004198611301986275.907
1004198702281986371.83
1004198705311986484.079
1004198708311987174.543
1004198711301987281.31
1004198802291987392.951
10041988053119874100.858
1004198808311988190.007
10041988113019882100.645
10041989022819883102.318
10041989053119884113.39

1st Quarter saleq = 1st Quarter saley
2nd Quarter saleq = 2nd Quarter saley - 1st Quarter saley
3rd Quarter saleq = 3rd Quarter saley - 2nd Quarter saley
4th Quarter saleq = 4th Quarter saley- 3rd Quarter saley

 

data a5; set a4;
by fyearq;
if fqtr=1 then cf1=saley;
if fqtr=2 then cf2=saley;
if fqtr=3 then cf3=saley;
if fqtr=4 then cf4=saley;
if fqtr=2 then saleq=cf2-cf1;
if fqtr=3 then saleq=cf3-cf2;
if fqtr=4 then saleq=cf4-cf3;
run;

 

Any help will be highly appreciated.

Thanks

Joon1

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

Try DIF with special handling of FIRST.<company>  Coding an IFN() function call ensures all arguments that are expressions will be evaluated and thus you don't need to worry about a conditional DIF() functional call that would compute difference from the wrong lag.

 

data want;
set have;
by gvkey fyearq fqtr; * only need gvkey, but other two ensures ERROR occurs if data is NOT sorted for quarter to quarter computation;
sale_qdif = ifn(first.gvkey, ., dif1(saley)); run;

 

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

Use the DIF function, defined as  DIF(x)=X-LAG(X):

 

data a5; 
  set a4;
  saleq=dif(saley);
  if fqtr=1 then saleq=saley;
run;

BTW, this code requires that each GVKEY begins with FQTR=1.   Otherwise the first record of an offending GVKEY would have values contaminated with a lagged saley value from the prior gvkey.

 

Edit note: Of course this also assumes data are sorted by gvkey/datadate.  And that there are no missing quarters.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
andreas_lds
Jade | Level 19

Please post the data in usable form, being a member of the community for three years, you should know that excel-files are not usable for many other members. Also add what you expect as result using the data provided.

RichardDeVen
Barite | Level 11

Try DIF with special handling of FIRST.<company>  Coding an IFN() function call ensures all arguments that are expressions will be evaluated and thus you don't need to worry about a conditional DIF() functional call that would compute difference from the wrong lag.

 

data want;
set have;
by gvkey fyearq fqtr; * only need gvkey, but other two ensures ERROR occurs if data is NOT sorted for quarter to quarter computation;
sale_qdif = ifn(first.gvkey, ., dif1(saley)); run;

 

joon1
Quartz | Level 8

Thank you so much for your help, Richard. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 779 views
  • 1 like
  • 4 in conversation