BookmarkSubscribeRSS Feed
michellel
Calcite | Level 5

Hi,

I have a data set looking like listed below. Every student has unique student id. For each student, there may different test id. For the same student and same test id, the variable line_id will list as 1,2,...,n. I am interested in the variable X. I want to check if contents in variable X are same for the same student and same test_id. If so for all of the data, which I think it should be, next step will be easily delete the observations for line_id not equal to 1 to drop the duplicates. Could anyone tell me how to program? Thanks a lot!

student_id     test_id     line_id     X

1                    2345          1         

1                    2345          2

1                    3468          1

1                    3468          2

1                    3468          3

2                    7896          1

2                    1234          1

2                    1234          2

2                    3452          1

2                    3452          2

3 REPLIES 3
Reeza
Super User

Look into proc sort and the dupkey/duprecs and dupout options.

pradeepalankar
Obsidian | Level 7

Duplicates can be removed as suggested by Reeza.

data students;

input student_id     test_id     line_id;

cards;

1                    2345          1

1                    2345          2

1                    3468          1

1                    3468          2

1                    3468          3

2                    7896          1

2                    1234          1

2                    1234          2

2                    3452          1

2                    3452          2

;

proc sort data=students dupout=student_dups nodupkey;

by student_id test_id;

run;

but what you want in variable X is not clear, please provide example output dataset.

Astounding
PROC Star

Here's one way;

data want;

   output_all=0;

   do until (last.test_id);

      set have;

      by student_id test_id;

      if first.test_id then first_x=x;

      if x ne first_x then output_all=1;

   end;

   do until (last.test_id);

      set have;

      by student_id test_id;

      if output_all=1 then output;

      else if line_id=1 then output;

   end;

run;

Both DO loops read the same observations (all that exist for a student_id test_id combination).  The top loop determines whether X changes within the group.  The bottom loop outputs accordingly.

Good luck.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 921 views
  • 0 likes
  • 4 in conversation