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

Hi all

Please need to simplify the following code or alternative more efficient way to do it:

proc sort data=example ;
     by ISIN datadate;
run;

data example ;
     set example;
	 lag_ISIN=lag(ISIN);
	 lag_PRCCD=lag(PRCCD);
	 If lag_ISIN=ISIN then RET=PRCCD/lag_PRCCD-1;else RET=.;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data example ;
     set example;
by ISIN;
	
RET=PRCCD/lag_PRCCD - 1;
if first.ISIN then RET=.;
format ret percent12.1;
run;

There really isn't much to simplify here that I can see, you still end up with roughly the same lines of code. You could possibly remove one by using IFN() but I don't think you gain any efficiency doing so. 

 


@georgel wrote:

Hi all

Please need to simplify the following code or alternative more efficient way to do it:

proc sort data=example ;
     by ISIN datadate;
run;

data example ;
     set example;
	 lag_ISIN=lag(ISIN);
	 lag_PRCCD=lag(PRCCD);
	 If lag_ISIN=ISIN then RET=PRCCD/lag_PRCCD-1;else RET=.;
run;

 

View solution in original post

2 REPLIES 2
Reeza
Super User
data example ;
     set example;
by ISIN;
	
RET=PRCCD/lag_PRCCD - 1;
if first.ISIN then RET=.;
format ret percent12.1;
run;

There really isn't much to simplify here that I can see, you still end up with roughly the same lines of code. You could possibly remove one by using IFN() but I don't think you gain any efficiency doing so. 

 


@georgel wrote:

Hi all

Please need to simplify the following code or alternative more efficient way to do it:

proc sort data=example ;
     by ISIN datadate;
run;

data example ;
     set example;
	 lag_ISIN=lag(ISIN);
	 lag_PRCCD=lag(PRCCD);
	 If lag_ISIN=ISIN then RET=PRCCD/lag_PRCCD-1;else RET=.;
run;

 

Phil_NZ
Barite | Level 11

@georgel 

It seems that you are dealing with data from CRSP or Compustat in Finance

I agree with solution from @Reeza , just want to highlight that you should calculate the return first then set the if later like this answer (because the way "if" plays quite tricky).

You may receive  a lot of notes that "Missing ..." in your log, but it makes sense and (maybe) safe because there should be some missing obs somewhere in your dataset. If you want to be over this note, the norm is to set the full condition if you want to clarify any NOTE.

For example:

 

lag_prccd=lag(prccd);

 

may cause the note" missing..."

but 

lag_prccd=ifn(prccd<=0,., lag(prccd));

won't result in such a note.

Remember Compustat put the average of Bid/Ask spread as closing price (prccd in your case) and missing prccd will be set as 0 rather than ".", so be careful when dealing with such a variable in your dataset

 

Warm regards,

Phil.

 

 

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1857 views
  • 1 like
  • 3 in conversation