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

i have

datadate

20040101

20040102

and so on until 20131231.

I basically wan to keep rows with the last date of the year, for each year

there are some caveats:

some years last day with the data might be 20131229 for example not 20131231.

i wan to end up with

20041231

20051231

20061231 and so on and if 31st is not observable the latest date possible of that year.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

1. convert your field to an actual date field, slchen has posted a method for this above.

2. Create a year variable

3. Sort by Date/Year

4. Use last. processing to get the last one.

data part1;

set have;

date_var=input(year, yymmdd8.);

format date_var date9.;

year=year(date_var);

run;

proc sort data= part1;

by company year date;

run;

data want;

set have;

by company year;

if last.year;

run;

View solution in original post

5 REPLIES 5
slchen
Lapis Lazuli | Level 10

data have;

input date yymmdd8.;

format date yymmddn8.;

cards;

20040102

20040312

20040616

20041231

20060419

20061229

20080223

20081231

;

run;

proc sql;

  select max(date) as date format=yymmddn8. from have group by year(date);

  quit;

aarony
Obsidian | Level 7

sorry i should have been more specific

i have company names as well for this.

so basically

datadate

20040101

20040102

and so on until 20131231.

for companies a to z.

i want to keep the last date of each year for each company.

how may i tackle this issue? thank you in advance.

Reeza
Super User

1. convert your field to an actual date field, slchen has posted a method for this above.

2. Create a year variable

3. Sort by Date/Year

4. Use last. processing to get the last one.

data part1;

set have;

date_var=input(year, yymmdd8.);

format date_var date9.;

year=year(date_var);

run;

proc sort data= part1;

by company year date;

run;

data want;

set have;

by company year;

if last.year;

run;

aarony
Obsidian | Level 7

you are a magician reeza. thank you!

Ksharp
Super User
data have;
input date yymmdd8.;
format date yymmddn8.;
cards;
20040102
20040312
20040616
20041231
20060419
20061229
20080223
20081231
;
run;
 data last;
  set have;
  if date=mdy(1,1,year(date)+1)-1;
run;

Xia Keshan

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2584 views
  • 1 like
  • 4 in conversation