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.

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
  • 939 views
  • 1 like
  • 3 in conversation