DATA Step, Macro, Functions and more

Write only the last observation.

Reply
N/A
Posts: 0

Write only the last observation.

My original SAS data set, DS1, has one variabel, Year.

My first code looks like this:

data DS2;
set DS1;
keep year count;
if year=1996 then count+1;
run;

The program runs and I can check that count has incremented for each year=1996.

Then I tried with this code:

data DS2;
keep year count;
set DS1 end=last;
if last then output;
run;

I expected to have DS2 with one row, and with year value equal to DS1:s last year value and the same value for count as in the first case.
But now DS2 has no observations.

What's wrong with my code?

Anne
N/A
Posts: 0

Re: Write only the last observation.

Posted in reply to deleted_user
Editing:

Then I tried with this code:

data DS2;
keep year count;
set DS1 end=last;
if year=1996 then count+1;
if last then output;
run;
N/A
Posts: 0

Re: Write only the last observation.

Posted in reply to deleted_user
Are you sure?

I ran this code and I get an observation at the end.

data ds1;
input year;
cards;
1995
1996
1995
1997
1996
1990
;

data DS2;
keep year count;
set DS1 end=last;
if year=1996 then count+1;
if last then output;
run;
Ask a Question
Discussion stats
  • 2 replies
  • 1357 views
  • 0 likes
  • 1 in conversation