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

If I have a wide data set where each row represents a student and the columns show the years within which they got punished eg:

Student Year1 Year2 Year3 Year4

A             1         1         -         1

B             -         -          1         1

C             1        1          1         1

 

So I need a code that can show me the number of students who have been punished across 4,3,2 consecutive years.

 

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @Zozo_star 

 

Please try this. Does the following output meet your needs?

data have;
	infile datalines dsd missover dlm=" ";
	input Student $ Year1 Year2 Year3 Year4;
	datalines;
A 1 1  1
B   1 1
C 1 1 1 1
;
run;

proc transpose data=have out=have_tr(rename=(col1=Punished)) name=Year ;
	var year:;
	by student;
run;

data have_consec;
	do i=1 until (last.student);
		set have_tr;
		by student;
		count+1;
		if missing(Punished) then count=0;
		else if first.student then count=1;
	end;
run;

proc means data=have_consec max noprint;
	var count;
	class student;
	ways 1;
	output out=want (drop=_:) max=max_Yr_consec;
run;

proc freq data=want;
	title "Number of students who have been punished across 4,3,2 consecutive years";
	table max_Yr_consec / nocum nopercent;
	label max_Yr_consec="Consecutive years";
run;

Output:

Capture d’écran 2020-05-22 à 10.53.13.png

 

Best, 

View solution in original post

4 REPLIES 4
ed_sas_member
Meteorite | Level 14

Hi @Zozo_star 

 

Please try this. Does the following output meet your needs?

data have;
	infile datalines dsd missover dlm=" ";
	input Student $ Year1 Year2 Year3 Year4;
	datalines;
A 1 1  1
B   1 1
C 1 1 1 1
;
run;

proc transpose data=have out=have_tr(rename=(col1=Punished)) name=Year ;
	var year:;
	by student;
run;

data have_consec;
	do i=1 until (last.student);
		set have_tr;
		by student;
		count+1;
		if missing(Punished) then count=0;
		else if first.student then count=1;
	end;
run;

proc means data=have_consec max noprint;
	var count;
	class student;
	ways 1;
	output out=want (drop=_:) max=max_Yr_consec;
run;

proc freq data=want;
	title "Number of students who have been punished across 4,3,2 consecutive years";
	table max_Yr_consec / nocum nopercent;
	label max_Yr_consec="Consecutive years";
run;

Output:

Capture d’écran 2020-05-22 à 10.53.13.png

 

Best, 

ballardw
Super User

Monkey wrench question:

Suppose you have 6 years of data and one student has 2 consecutive years and 3 consecutive years with a year without in between. Which gets counted?

Zozo_star
Calcite | Level 5
Once I knew the frequencies as suggested above, I then set a condition to pick all with any 2, 3 etc.

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