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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 615 views
  • 0 likes
  • 4 in conversation