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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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