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

Hi....I am trying to create the variable Final from the Decision variable. For each ID and Color group, if there is 'Yes' in the group, then Final is 'Yes' for the last row in the group and all other entries for Final is 'No'. Any suggestions on the best way to do this...Thanks

data Have;
    format ID best12. Color $char6. Part $char1. Decision $char3. Final $char3.;
	input ID : best32. Color : $char6. Part : $char1. Decision : $char3. Final : $char3.;
cards;
1 Yellow A Yes No
1 Yellow B No No
1 Yellow C No Yes
2 Blue A No No
2 Blue B Yes No
2 Blue C No Yes
3 Red A No No
3 Red B No No
3 Red C Yes Yes
4 Orange A Yes No
4 Orange B No Yes
5 Green B No No
5 Green C Yes Yes
6 Red A No No
6 Red C No No
;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    by id color;
    length final $ 3;
    retain flag;
    if first.color then flag=0;
    if decision='Yes' then flag=1;
    if not last.color or (last.color and flag=0) then final='No';
    else if last.color and flag=1 then final='Yes';
    drop flag;
run;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
data want;
    set have;
    by id color;
    length final $ 3;
    retain flag;
    if first.color then flag=0;
    if decision='Yes' then flag=1;
    if not last.color or (last.color and flag=0) then final='No';
    else if last.color and flag=1 then final='Yes';
    drop flag;
run;
--
Paige Miller
twildone
Pyrite | Level 9
Thanks Paige!!!

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 575 views
  • 0 likes
  • 2 in conversation