Solved
Contributor
Posts: 62

# RETAIN question

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!!!!

Accepted Solutions
Solution
‎03-03-2016 10:59 AM
Super User
Posts: 13,583

## Re: RETAIN question

[ Edited ]

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;

All Replies
Super User
Posts: 5,888

## Re: RETAIN question

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
Solution
‎03-03-2016 10:59 AM
Super User
Posts: 13,583

## Re: RETAIN question

[ Edited ]

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;

Contributor
Posts: 62

## Re: RETAIN question

Perfect!!!! thanks!!!

Spoiler

🔒 This topic is solved and locked.