BookmarkSubscribeRSS Feed
PDevi
Fluorite | Level 6

Edited  to add additional requirements. Sorry for the confusion.

 

I have a data set that looks like below:

 

Sample data:

 

IdTermAudit_typeProgLevelLoad
1Fall 20151ABCD01F
1Fall 20152ABCD01F
1Winter 20161ABCD02F
1Winter 20162ABCD02F
1Fall 20161ABCD03F
1Fall 20162ABCD03F
1Winter 20171ABCD04F
1Winter 20172ABCD04F
2Fall 20121LMNO01F
2Fall 20122LMNO01F
2Winter 20131LMNO02F
2Winter 20132LMNO02F
2Spring 20131LMNO03P
2Fall 20131LMNO03F
2Fall 20132LMNO03F
2Winter20141WXYZ01F
2Spring 20141PQRS01P
2Spring 20142PQRS01P
2Fall 20141PQRS01P
2Winter 20151PQRS01F
2Winter 20152PQRS01F
3Fall 20161EFGH01F
3Fall 20162EFGH01F
3Winter 20171EFGH02F
3Spring 20171EFGH02P
3Spring 20172EFGH02P
3Fall 20171EFGH02F
3Fall 20172EFGH02F

 

What I need is to select the next two records, for when the student comes back after level '01' in the first term, such that the audit_type is always '2'. And this has to be for every id, term, program combination.

 

For id #2, the student continues and moves forward in the first program. The second program is discontinued and s/he changes it to a third program, which is continued. I need to bring in information for the first and the third programs both.

 

So basically, my selection needs to be as below:

 

Desired Solution:

 

IdTermAudit_typeProgLevelLoad
1Winter 20162ABCD02F
1Fall 20162ABCD03F
2Winter 20132LMNO02F
2Fall 20132LMNO03F
2Spring 20142PQRS01P
2Winter 20152PQRS01F
3Spring 20172EFGH02P
3Fall 20172EFGH02F

 

I would appreciate it if someone could please help. Thanks in advance.

4 REPLIES 4
Astounding
PROC Star

This might look easier in the morning, but it should work:

 

data want;

set have;

by id term notsorted;

if first.id then do;

   term_count=1;

   output_count=0;

end;

else if first.term then term_count + 1;

if term_count > 1 and output_count < 2 and audit_type=2 then do;

   output;

   output_count + 1;

end;

drop term_count output_count;

run;

PDevi
Fluorite | Level 6

@Astounding It does work, thank you very much. But I have an added layer of requirement that I had forgotten to include in the sample data. When I tested your code, that's when I realized my mistake. The sample data and the desired outcome have been edited. I am sorry for the over sight.

Astounding
PROC Star

If I understand the new requirements properly, a very similar program can do the trick.

 

proc sort data=have;

   by id prog;

run;

 

data want;

set have;

by id prog term notsorted;

if first.prog then do;

   term_count=1;

   output_count=0;

end;

else if first.term then term_count + 1;

if term_count > 1 and output_count < 2 and audit_type=2 then do;

   output;

   output_count + 1;

end;

drop term_count output_count;

run;

 

It's untested at this point, so see what you get from it.

ChrisNZ
Tourmaline | Level 20

Like this?

data WANT;
  set HAVE;
  by ID;
  if first.ID then call missing(AFTER_FIRST,OUTPUT_NO);
  if ID=lag(ID) and TERM ne lag(TERM) then AFTER_FIRST+1;
  if AUDIT_TYPE=2 & AFTER_FIRST then do;
    OUTPUT_NO+1;
    if OUTPUT_NO in (1,2) then output;
  end;
run;
id Term y Audit_type Prog Level Load
1 Winter 2016 2 ABCD 2 F
1 Fall 2016 2 ABCD 3 F
2 Spring 2014 2 PQRS 1 P
2 Winter 2015 2 PQRS 1 F
3 Spring 2017 2 EFGH 2 P
3 Fall 2017 2 EFGH 2 F

 

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