BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello,

I have the following data:
input_table (ordered by customernumber, code number):

customernumber code_number

1 a1
1 b1
1 c1
2 a1
2 c1
3 c1


I'd like to use lag to look 2 rows prior, determine if the customernumber is the same for the prior 2 customernumber rows, then look at the code number, and determine if I'm on code number c1, and if so look back at the 2 prior code number rows are a1 and b1. If so, this meets the criteria I'm looking for. As an example, customernumber 1 is the only one above that matches this criteria. I'm using the following SAS code to do this, using lag. Please let me know if this looks good, thx!


libname sassvrtable 'my/sasdatabase/table'
data output_data;
set sassvrtable.input_table;

\*prev custnumber*\
lag_data_custnum = lag(customernumber);

\*2nd prev custnumber*\
lag_data_custnum2 = lag(lag_data_custnum);

\*prev codenumber*\
lag_code_number = lag(code_number);
\*2 prev codenumber*\
lag_code_number_2 = lag(lag_code_number);

\*IF MEETS MY CRITERIA*\
if lag_data_custnum = customernumber
and lag_data_custnum2 = customernumber
and code_number = c1
and lag_code_number = b1
and lag_code_number_2 = a1
then match = 'TRUE';

Proc Print data = output_data;
run;
2 REPLIES 2
milts
Pyrite | Level 9
Hi Scott,

Try using lag2 instead of lag

Milton
deleted_user
Not applicable
Thank you, this will work.

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
  • 633 views
  • 0 likes
  • 2 in conversation