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

Hi,

i have this table:

KEY CAU
2335083090018500 M88
33111356428352800 M88
33111275803210300 M88
33111381165566500 M88
33111708996242600 M88
33111708996242600 M90
2214196DL51H86501 M88
2214196DL51H86501 M90
33111230975677500 M88
33111230975677500 M90
3311359190969000 M88
3311359190969000 M90
33111380983028300 M88
33111380983028300 M90
503440K7QD3L18858 M88
22158083090018500 M88
503440K7QD3L18858 M90
2215083090018500 M90
33111208999140500 M88

 

I want create a new column (RESULT) with value "YES" when the value of column key is equal to previous value and the value  of column CAU is equal at 'M90' and  previous value is  equal to 'M88', this is example:

  

KEY CAU RESULT
2335083090018500 M88  
33111356428352800 M88  
33111275803210300 M88  
33111381165566500 M88  
33111708996242600 M88  
33111708996242600 M90 YES
2214196DL51H86501 M88  
2214196DL51H86501 M90 YES
33111230975677500 M88  
33111230975677500 M90 YES
3311359190969000 M88  
3311359190969000 M90 YES
33111380983028300 M88  
33111380983028300 M90 YES
503440K7QD3L18858 M88  
22158083090018500 M88  
503440K7QD3L18858 M90  
2215083090018500 M90  
33111208999140500 M88  

 

Can someone give me a help to create a data step with retain function?

Thanks!!!!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You could use RETAIN but this is what the LAG function which looks at previous values.

 

data want;

   set have;

   Lkey = lag(key);

   LCAU = lag(CAU);

   if Lkey=key and CAU='M90' and LCau='M88' then Result='YES';

   drop Lkey Lcau;

run; 

View solution in original post

3 REPLIES 3
LinusH
Tourmaline | Level 20

Use retain to create two new variables, like key_ret and cau_ret.

Assign them the current observation values last in the data step, after any conditional assignments.

Data never sleeps
ballardw
Super User

You could use RETAIN but this is what the LAG function which looks at previous values.

 

data want;

   set have;

   Lkey = lag(key);

   LCAU = lag(CAU);

   if Lkey=key and CAU='M90' and LCau='M88' then Result='YES';

   drop Lkey Lcau;

run; 

Cello23
Quartz | Level 8

Perfect!!!! thanks!!!

Spoiler
 

Smiley Very Happy

 

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!

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
  • 3 replies
  • 917 views
  • 2 likes
  • 3 in conversation