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

I need output for the below yellow last column like this. Thanks in advance. 

 

IDRace1Race2Race3Race4Output
1001asianhispanicwhite Mutiple
1002    "Missing"
1003asian   asian
1004  AsianHispaniMutiple
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Hello @tsureshinvites  and welcome to the SAS Community 🙂

 

You can do something like this

 

data have;
input ID (Race1-Race4)(:$10.);
infile datalines dsd dlm=',';
datalines;
1001,asian,hispanic,white,
1002,,,,
1003,asian,,,
1004,,,Asian,Hispanic
;

data want(drop=a);
    set have;
    a=cmiss(of Race:);
    if a in (1, 2) then output='Multiple';
    else if a=3    then output=coalescec(of Race:);
    else if a=4    then output='Missing'; 
run;

Result:

 

 

ID    Race1    Race2    Race3    Race4     Output
001   asian    hispanic white              Multiple
1002                                       Missing
1003  asian                                asian
1004           Asian             Hispanic  Multiple

 

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Hello @tsureshinvites  and welcome to the SAS Community 🙂

 

You can do something like this

 

data have;
input ID (Race1-Race4)(:$10.);
infile datalines dsd dlm=',';
datalines;
1001,asian,hispanic,white,
1002,,,,
1003,asian,,,
1004,,,Asian,Hispanic
;

data want(drop=a);
    set have;
    a=cmiss(of Race:);
    if a in (1, 2) then output='Multiple';
    else if a=3    then output=coalescec(of Race:);
    else if a=4    then output='Missing'; 
run;

Result:

 

 

ID    Race1    Race2    Race3    Race4     Output
001   asian    hispanic white              Multiple
1002                                       Missing
1003  asian                                asian
1004           Asian             Hispanic  Multiple

 

Ksharp
Super User
data have;
input ID (Race1-Race4)(:$10.);
infile datalines dsd dlm=',';
datalines;
1001,asian,hispanic,white,
1002,,,,
1003,asian,,,
1004,,,Asian,Hispanic
;
data want;
 if _n_=1 then do;
  length key flag $ 40;
  declare hash h();
  h.definekey('key');
  h.definedone();
 end;
 set have;
 h.clear();
 array x{*} $ race:;
 do i=1 to dim(x);
   if not missing(x{i}) then do;key=x{i};h.ref();end;
 end;
 n=h.num_items;
 if n=0 then flag='Missing';
  else if n=1 then flag=coalescec(of x{*});
   else flag='Multiple';
 run;
 proc print;run;
 
Reeza
Super User
array _race(*) race1-race4;
n_values = dim(_race) - cmiss(of _race(*));

if n_values > 1 then output = 'Multiple';
else if n_values = 1 then output = coalescec(of _race(*));
else if n_values = 0 then output = 'Missing';
else output = 'CHECKME';

Does't matter how many race variables you have with this version. 

Use DIM() to get number of values, CMISS() to find number of missing and then the difference is how many values are filled in.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 550 views
  • 3 likes
  • 4 in conversation