vendredi 24 février 2017

SAS retain statement

Suppose I have a dataset with three variables:
ID   Year   Status
1   2017   Y
1   2018   N
1   2017   N
1   2018   Y
2   2017   Y
2   2018      
2   2017   N
2   2018   N

I would like to create a fourth column called NEW which has three possible values ('Yonly' 'Nonly' and 'yesno'). In the example above the output will be:
ID   Year   Status   NEW
1   2017   Y   yesno
1   2018   N   yesno
1   2017   N   yesno
1   2018   Y   yesno
2   2017   Y   yesonly
2   2018         yesonly
2   2017   N   noonly
2   2018   N   noonly

Note: could have missing data. My solution so far is wrong:

retain tmp '';
by ID Year;
if Status='Y' then tmp='Yonly';
if Status='N' then tmp='Nonly';
if tmp='Yonly and Status='N' then tmp='yesno';
  if tmp='Nonly and Status='Y' then tmp='yesno';
if last.Year=1 then NEW=tmp;

Please help? Any method will do, you don't have to use RETAIN.

Aucun commentaire:

Enregistrer un commentaire