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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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