Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: RETAIN question

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;

3 REPLIES 3
Tourmaline | Level 20

## 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
Super User

## Re: RETAIN question

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;

Quartz | Level 8

## Re: RETAIN question

Perfect!!!! thanks!!!

Spoiler

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