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

I have a dataset that looks like this:

 

Obs

ID

Indate

Outdate

Lines

1

1

17/08/10

20/08/10

1

2

1

17/08/10

22/08/10

2

3

2

06/01/11

10/01/11

1

4

3

15/08/10

15/08/10

1

5

3

15/08/10

17/08/10

2

6

3

15/08/10

25/08/10

3

7

3

15/08/10

21/09/10

4

8

4

17/06/12

17/06/12

1

9

5

26/01/11

27/01/11

1

10

6

06/11/12

06/11/12

1

11

6

14/12/12

14/12/12

1

12

6

17/11/14

17/11/14

1

13

6

17/11/14

18/11/14

1

14

7

30/10/10

31/10/10

1

15

7

30/10/10

01/11/10

2

 

And by using the statement:

 

proc sort data=have;

     by pnr indate;

run;

 

data have;

     set have; by pnr indate;

     LastLine=last.indate;

run;

 

I get this:

 

Obs

ID

Indate

Outdate

Lines

LastLine

1

1

17/08/10

20/08/10

1

0

2

1

17/08/10

22/08/10

2

1

3

2

06/01/11

10/01/11

1

1

4

3

15/08/10

15/08/10

1

0

5

3

15/08/10

17/08/10

2

0

6

3

15/08/10

25/08/10

3

0

7

3

15/08/10

21/09/10

4

1

8

4

17/06/12

17/06/12

1

1

9

5

26/01/11

27/01/11

1

1

10

6

06/11/12

06/11/12

1

1

11

6

14/12/12

14/12/12

1

1

12

6

17/11/14

17/11/14

1

0

13

6

17/11/14

18/11/14

1

1

14

7

30/10/10

31/10/10

1

0

15

7

30/10/10

01/11/10

2

1

 

It is correct until observation nr. 12. This should be 1 because Lines=1 in the next row... I've tried to sort by Outdate instead and I have used the lag(indate) but nothings works for me... How do I get it right? 

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

Use a view and create a new variable G(roup) that is incremented when lines eq 1.

 

data inout;
   infile cards dsd dlm='09'x firstobs=2;
   input id (indate outdate)(:ddmmyy.) lines;
   format indate outdate ddmmyy10.;
   cards;
ID	Indate	Outdate	Lines
1	17/08/10	20/08/10	1
1	17/08/10	22/08/10	2
2	6/1/2011	10/1/2011	1
3	15/08/10	15/08/10	1
3	15/08/10	17/08/10	2
3	15/08/10	25/08/10	3
3	15/08/10	21/09/10	4
4	17/06/12	17/06/12	1
5	26/01/11	27/01/11	1
6	6/11/2012	6/11/2012	1
6	14/12/12	14/12/12	1
6	17/11/14	17/11/14	1
6	17/11/14	18/11/14	1
7	30/10/10	31/10/10	1
7	30/10/10	1/11/2010	2
;;;;
   run;
proc print;
   run;
data inout2v / view=inout2v;
   set inout;
   by id;
   if first.id then g=0;
   if lines eq 1 then g+1;
   run;
data inout2;
   set inout2v;
   by id g;
   last = last.g;
   run;
proc print;
   run;

Capture.PNG

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Your code doesn't use lines at all it is just flagging the last indate.  Sort by all of the items in the group, and then use last.smallest group, for example (not tested as test data not as datastep):

proc sort data=have;
  by pnr indate lines;
run;
 
data have;
  set have; 
  by pnr indate lines;
  lastline=last.lines;
run;
Gothardt
Obsidian | Level 7

I've already tried this and it dose not work: all the values of last.line =1...

data_null__
Jade | Level 19

Use a view and create a new variable G(roup) that is incremented when lines eq 1.

 

data inout;
   infile cards dsd dlm='09'x firstobs=2;
   input id (indate outdate)(:ddmmyy.) lines;
   format indate outdate ddmmyy10.;
   cards;
ID	Indate	Outdate	Lines
1	17/08/10	20/08/10	1
1	17/08/10	22/08/10	2
2	6/1/2011	10/1/2011	1
3	15/08/10	15/08/10	1
3	15/08/10	17/08/10	2
3	15/08/10	25/08/10	3
3	15/08/10	21/09/10	4
4	17/06/12	17/06/12	1
5	26/01/11	27/01/11	1
6	6/11/2012	6/11/2012	1
6	14/12/12	14/12/12	1
6	17/11/14	17/11/14	1
6	17/11/14	18/11/14	1
7	30/10/10	31/10/10	1
7	30/10/10	1/11/2010	2
;;;;
   run;
proc print;
   run;
data inout2v / view=inout2v;
   set inout;
   by id;
   if first.id then g=0;
   if lines eq 1 then g+1;
   run;
data inout2;
   set inout2v;
   by id g;
   last = last.g;
   run;
proc print;
   run;

Capture.PNG

Gothardt
Obsidian | Level 7

This datastep dosen't work:

data inout2;
   set inout2;
   by id g;
   last = last.g;
   run;
proc print;

I get: 

ERROR: BY variable pnr is not on input data set

ERROR: BY variable is not on input data set

Gothardt
Obsidian | Level 7

I found out - it seems to work without the view-statement. Thanks a lot!

 

 

data_null__
Jade | Level 19

I changed to the view after I had use a data set.  The perils of a continuing SAS session.  I'll edit my reply.

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