BookmarkSubscribeRSS Feed
lutherh
Calcite | Level 5

Hi All,

 

I have a dataset with four columns, of which the middle two have character variables from which I would like to delete duplicates. The first column is an identifier column, 'GuestNo'. The second and third columns are character columns listing type of candy bought before and after a diet program. The second column is the type of candy a guest bought before a diet program, the third column is the type of candy a guest bought after a diet program. I only want to keep the rows which show the unique candy choices before and after the diet program.

 

My data currently looks like this:

 

GuestNo       Before        After        Cost

     1                  A                               3

     1                  A                              3.5  

     1                  A                               3  

     1                  B                               4

     1                  C                               5

     1                                   A            3.75

     1                                   A            3.75

     1                                   B             4.5

     1                                   D              7

     1                                   E              8

     2                  A                             3.25

     2                  F                             9.25

     2                                   F            9.50

     2                                   G             10

 

 

I want to know the amount of money guests spend on unique types of candy before and after the program. Thus, I would like to be able to create a new dataset that looks like this:

 

GuestNo       Before        After        Cost

 

     1                  C                               5

     1                                   D              7

     1                                   E              8

     2                  A                             3.25

     2                                   G             10

 

I have tried using a 'nodupkey' function, but it seems to only delete data from one column at a time, without the ability to read from multiple columns. Any help would be much apprecaited on the topic!

2 REPLIES 2
bnawrocki
Quartz | Level 8

Actually, NODUPKEY works on multiple columns also. If there's more than one record in which ALL the BY vars are the same, it will keep only the first of those records found. So, if you run -

PROC SORT NODUPKEY; 
   BY GUESTNO BEFORE AFTER;

- it should give you close to what you want.

 

I don't entirely understand the output you want, however -- why don't you want to keep row 4 - where CUSTNO=1, BEFORE=B, AFTER=blank? That particcular combination of values is not duplicated anywhere in the file, is it?

 

alooo
Calcite | Level 5

Hi I have encountered similar questions... have you figured it out at last? Thank you!

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!

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