DATA Step, Macro, Functions and more

Help with ARRAY, Loops.

Occasional Contributor
Posts: 13

Help with ARRAY, Loops.

I am tracking student cohorts over time to identify when students transfer out of the college and which terms they are co-enrolled at my college (HOME) and another institution (TRFINST) at the same time. This is a many to many match between the student’s HOME semesters and the TRFINST semesters.  I can identify the multiple semesters that
students were “co-enrolled” (enrolled at Home and a TRFINST in the same semester) and the semesters that students transferred to a TRFINST.  My problem is that project requirements are that after a student is identified as a student is identified as a Transfer Student,  that transfer semester is to be reported an no further tracked terms are to be reported.   I am
trying to revise a program that I use to track retention using the same type data but am bogged down on how to eliminate future values for students identified as Transfer students.  (i.e., student transfers and then comes back to HOME for a semester and later transfers to another college - no data after the first transfer is to be reported). I figure I can transpose the data results I have, unduplicate it,  and then remerge the data again.  But would like to do it without all that manipulation. So any ideas will be appreciated.

I have attached a program Headach.SAS and a sample SAS file (TEST_TRF_PROJ2).       ENRTERM(I) - "1" indicates the student was enrolled at HOME; TRFSEM(I)- "1"  indicates the student was enrolled at TRFINST; The variable index numbers are based on the student's cohort term and indicate the numbers of semesters since the cohort term.  Student 23 has become my “test” student and should have a “1” value for COENRSEM 3-7 and 0 for COENRSEM 9-15; and a “1” value for XSEMTRF9 and “0” for the XSEMTRF1-9 and 10-15.


Frequent Contributor
Posts: 130

Re: Help with ARRAY, Loops.

[ Edited ]
Posted in reply to madhatter

Hello @madhatter,


unfortunately I am not sure, what your exact requirements are.

The fields coho, cohoterm and enr were not explained so I did not use them any further.

I understand that you have 15 semesters of data?


Here would be my code that does give you per ssn (ssn is the student number?) and per semester:

If a student is enrolled at your home college and enrolled at a foreign college.

Plus the first occasion, when a student is enrolled only at a foreign college and no more lines for that student afterwards.


Given the numbers in your fields indicate the semesters and are in correct order (i.e. enrterm1 means enrolment in semester 1).


/* transpose data */
DATA have_1 (keep = ssn coho cohoterm semester enrol_home enrol_abroad);
	SET have;

DO I=1 TO 15;
  if trfsem(I) = 1 then enrol_abroad = 1; else enrol_abroad = 0;
  if enrterm(I) = 1 then enrol_home = 1; else enrol_home = 0;
  semester = I;


/* sort data for by-processing in next step */
proc sort data = have_1;
by ssn semester;

data want;
set have_1;
by ssn semester;
retain outputline;

  /* set "outputline" to make sure there is no more output after 
     a transfer student has been found and is in output once */
  if first.ssn then outputline = 1;

  /* criteria to find */
  if enrol_home and enrol_abroad and outputline then output; /* student enrolled home and abroad */
  else if not enrol_home and enrol_abroad and outputline /* transfer student */
         then do;
                outputline = 0;

  drop outputline; /* no need to output that */

Not sure, if this meets your requirements. Let me know if you had something else in mind.


If you need more information, you can always edit the logic in the last data step. For instance, create new columns like 'transfer_student', 'fist_semester_abroad', etc.

By using the retain statement, you can determine further information per student, as desired.




Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation