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

I realize this is a very easy concept, but I'm having trouble assigning values to a new column when my logic involves 'if always' or 'if ever'. For example, in the following sample dataset:

 

ID            Status            UpdStatus

1                  yes                 yes

1                  yes                 yes

1                  no                   yes

2                  no                    no

3                  no                    no

4                 yes                  yes

4                 no                   yes

 

I am trying to create a new column, where for a given ID if status is ever 'yes' then UpdStatus = 'yes' otherwise UpdStatus = 'no'. [Alternatively, if status is always 'no' then UpdStatus = 'no' else 'yes']. I've written sample attempts below, but I can't figure out the proper language to convey the 'ever' or 'always'.

 

data want;

set have;

format UpdStatus $3.;

if Status="(ever) yes" then UpdStatus = "yes";

else UpdStatus = "no";

run;

 

or:

 

data want;

set have;

format UpdStatus $3.;

if Status ="(always) no" then UpdStatus="no";

else UpdStatus ="yes";

run;

 

Thanks in advance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @bretthouston 

 

This assumes your dataset is sorted by ID as your sample suggests

 


data have;
input ID            Status       $;   
cards;
1                  yes                 yes
1                  yes                 yes
1                  no                   yes
2                  no                    no
3                  no                    no
4                 yes                  yes
4                 no                   yes
;

data want;
merge have have(in= b where=(status='yes'));
by id;
length updtd $3;
if b then updtd='yes';
else updtd='No';
run;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

Hi @bretthouston 

 

This assumes your dataset is sorted by ID as your sample suggests

 


data have;
input ID            Status       $;   
cards;
1                  yes                 yes
1                  yes                 yes
1                  no                   yes
2                  no                    no
3                  no                    no
4                 yes                  yes
4                 no                   yes
;

data want;
merge have have(in= b where=(status='yes'));
by id;
length updtd $3;
if b then updtd='yes';
else updtd='No';
run;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 636 views
  • 1 like
  • 2 in conversation