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

I have below piece of data and I want output in such a way that if a person has multiple rows of data then first blank value for cond_cd is removed from the output but if we have single row of data of person then blank value of cond_cd should remain. 

 

data one;
input @001 FirstName $10.
@011 LastName $10.
@021 cond_cd $6.
;
datalines;
JOHN MCCARTHY
JOHN MCCARTHY TGCHS
JOHN MCCARTHY abcde
Akshay KUMAR
Akshay KUMAR TGCHS
SMITA PAREKH
;
run;

 

Desired output =>

 

FirstName LastName Cond_cd

JOHN MCCARTHY TGCHS
JOHN MCCARTHY abcde
Akshay KUMAR TGCHS
SMITA PAREKH

1 ACCEPTED SOLUTION

Accepted Solutions
7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

What if you had several blank values for some name?

PeterClemmensen
Tourmaline | Level 20

So if this was in your data

 

JOHN MCCARTHY
JOHN MCCARTHY
JOHN MCCARTHY TGCHS
JOHN MCCARTHY abcde

 

you would want to keep all 4 obs?

PeterClemmensen
Tourmaline | Level 20

One way. Provided that your data is gruoped by Names

 

data one;
input FirstName $ 1-6 LastName $ 8-15 cond_cd $ 17-21;
datalines;
JOHN   MCCARTHY      
JOHN   MCCARTHY TGCHS
JOHN   MCCARTHY abcde
Akshay KUMAR         
Akshay KUMAR    TGCHS
SMITA  PAREKH        
;

data want;
    do _N_=1 by 1 until (last.LastName);
        set one;
        by LastName notsorted;
    end;
    do until (last.LastName);
        set one;
        by LastName notsorted;
        if _N_=1 | (not missing(cond_cd) & _N_>1) then output;
    end;
run;

 

Result:

 

FirstName   LastName   cond_cd
JOHN        MCCARTHY   TGCHS
JOHN        MCCARTHY   abcde
Akshay      KUMAR      TGCHS
SMITA       PAREKH          
novinosrin
Tourmaline | Level 20

data one;
infile cards truncover;
input FirstName :$10.
 LastName : $10.
 cond_cd : $6.
;
datalines;
JOHN MCCARTHY
JOHN MCCARTHY TGCHS
JOHN MCCARTHY abcde
Akshay KUMAR
Akshay KUMAR TGCHS
SMITA PAREKH
;
run;

proc sql;
create table want as
select *
from one
group by FirstName,LastName
having count(*)>1 and cond_cd ne ' ' or count(*)=1;
quit; 
 
Ksharp
Super User

data one;
infile cards truncover;
input FirstName :$10.
 LastName : $10.
 cond_cd : $6.
;
datalines;
JOHN MCCARTHY
JOHN MCCARTHY TGCHS
JOHN MCCARTHY abcde
Akshay KUMAR
Akshay KUMAR TGCHS
SMITA PAREKH
;
run;
proc sort data=one;
by FirstName LastName;
run;
data want;
 set one;
 by FirstName LastName;
 if not (first.LastName and last.LastName) and missing(cond_cd) then delete;
 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!

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
  • 7 replies
  • 996 views
  • 0 likes
  • 4 in conversation