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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 1 reply
  • 383 views
  • 0 likes
  • 2 in conversation