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

Hello world,

I need some help collapsing data into a single row. I've created a data set that creates a flag for the test each student has taken (see table). The problem is I need this data to be one row per student and can't figure out how to code this properly. I've provided an example below.

Original Data:

Student Test Flag A Flag BFlag C
Carl A 100
Carl B010
Tito A100
TitoB 010
TitoC001

Desired Output Data:

Student Flag A Flag B Flag C
Carl 110
Tito111

Is there a sas procedure I could use in this situation? Any help would be much appreciated.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Take the maximum for each flag over all records for that student.

proc means data=have nway ;

by student;

var flag: ;

output want  max= ;

run;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Take the maximum for each flag over all records for that student.

proc means data=have nway ;

by student;

var flag: ;

output want  max= ;

run;

Jsendzik
Fluorite | Level 6

Thank you

UrvishShah
Fluorite | Level 6

Hi,

Try with the following code...Although it is hard coded at one place to recreate the flags,it meets your requirement...

data have;

    input student $ test $ flag1 - flag3;

      cards4;

Carl A 1 0 0

Carl B 0 1 0

Tito A 1 0 0

Tito B 0 1 0

Tito C 0 0 1

;;;;

proc sql noprint;

   create table have1 as

   select student,count(student) as no_of_student

   from have

   group by 1;

   select max(no_of_student) into :max  

   from have1;

quit;

data want(drop = i);

   set have1;

   array flag(&max.);

   do i = 1 to &max.;

      flag(i) = 1;

        if no_of_student < &max. then do;

      flag3 = 0;

        end;

        /*Based on your requirement, add more flags*/

   end;

run;

-Urvish

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1569 views
  • 1 like
  • 3 in conversation