BookmarkSubscribeRSS Feed
Zhongda
Fluorite | Level 6

Dear All, 

 

I was trying to create a new variable , called lag_ar.

 

here is my original dataset:

PERMNOEVTDATEEVTTIMEcumulative_arprogram_codecounter_overall
100018/1/1150.93328421
100014/2/1250.9801622
100027/6/9851.1444421
1000210/27/9850.94716722
100204/6/8751.00391231
1002010/21/8750.89800732
1002012/11/8751.03288833
100204/17/9051.02332521
100204/24/9051.06357622

 

my code is 

proc sort data = datasetname;
by permno evtdate program_code;
run;
data within_lag;
set within;
by  permno evtdate program_code;
cum_ar_lag1 = ifn(not(first.permno),lag(cumulative_ar),.);

run;

 

but with my code, the result is not right;

take permno 10020 for example,

my code gives the result as below:

100204/6/8751.00391231 
1002010/21/8750.898007321.003912
1002012/11/8751.032888330.898007
100204/17/9051.023325211.032888
100204/24/9051.063576221.023325

however, for the 4th obs, the lag_ar should be ".", but it still grab the value from the 3rd observation. however, they are not in the same program code, which it should not grab the value and assign it there.

the right result I expect is as below:

the lag is supposed to do within same permno and same program_code.

PERMNOEVTDATEEVTTIMEcumulative_arprogram_codecounter_overalllag_ar
100204/6/8751.00391231 
1002010/21/8750.898007321.003912
1002012/11/8751.032888330.898007
100204/17/9051.02332521 
100204/24/9051.063576221.023325

 

I know I can fix my wrong result by assign a missing value to all obs that counter_overall is 1, but I really want to figure out why my 

by statement does not work.

 

Thank you so much again.

Zhongda

1 REPLY 1

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 439 views
  • 0 likes
  • 2 in conversation