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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1204 views
  • 2 likes
  • 3 in conversation