DATA Step, Macro, Functions and more

if first variable by another group

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

if first variable by another group

The table i have as below:

 

id        term      course         ind    

2         2016      ENC10        I         

2         2017      ENC10        E        

5        2010       MAT12       E          

5        2012       MAT12      I        

6        2011       STA11        E         

6        2013      STA11         I         

6       2015       STA11         E         

 

Table should be sot like this:

 

proc  sort data = work.a;

by id course term;run;

 

I neen only same id and course sort by term; last 'ind' should be 'E', first or second ind should be 'I'

So the table should be like below

 

id        term      course         ind    

2         2016      ENC10        I         

2         2017      ENC10        E         

6        2011       STA11        E         

6        2013      STA11         I         

6       2015       STA11         E         


Accepted Solutions
Solution
‎03-07-2017 09:45 AM
Super User
Super User
Posts: 7,413

Re: if first variable by another group

Again, post test data in the form of a datastep, I am not here to type in test data.  As such the logic would be something like:

data have;
set your_data;
by id; if (first.id or lag(first.id)) and ind="I" then k1=1; if last.id and ind="E" then k2=1; run; proc sql; create table WANT as select * from HAVE where ID in (select distinct ID from HAVE where k1 and k2); quit;

View solution in original post


All Replies
Super User
Posts: 6,954

Re: if first variable by another group

You have repeatedly been shown how to post data in a data step, and repeatedly been asked by users to do so.

DO SO NOW.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎03-07-2017 09:45 AM
Super User
Super User
Posts: 7,413

Re: if first variable by another group

Again, post test data in the form of a datastep, I am not here to type in test data.  As such the logic would be something like:

data have;
set your_data;
by id; if (first.id or lag(first.id)) and ind="I" then k1=1; if last.id and ind="E" then k2=1; run; proc sql; create table WANT as select * from HAVE where ID in (select distinct ID from HAVE where k1 and k2); quit;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 247 views
  • 0 likes
  • 3 in conversation