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

I am using the code suggested in one of the answers as I want to get the first and last date of a country.

data get_first_and_last;

  set master_table;

  by ID Date;

  if first.date or last.Date then output;

run;

However, I still get the dates in between and I'm replacint ID by country

I have the following observations

country      date

countrya     01jan00

countrya     01feb00

countrya     01feb00

countrya     01mar00

etc ...

countrya     01nov11

How do I get only the country and  01jan00 and 01nov11? Is there any other way. I have a lot of observations  and variables and I only choose the country and the date that is why I have repeated dates.

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

data have;

informat date date7.;

format date date7.;

input country $ date;

cards;

countrya     01jan00

countrya     01feb00

countrya     01feb00

countrya     01mar00

countrya     01nov11

countryb     01jan00

countryb     01feb00

countryb     01feb00

countryb     01mar00

countryb     01nov11

;

proc sort; by country date;

data want;

  set have;

  by country;

  if first.country or last.country;

run;

proc print; run;

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

Change two lines in your code:

  by ID;

  if first.ID or last.ID then output;


Linlin
Lapis Lazuli | Level 10

data have;

informat date date7.;

format date date7.;

input country $ date;

cards;

countrya     01jan00

countrya     01feb00

countrya     01feb00

countrya     01mar00

countrya     01nov11

countryb     01jan00

countryb     01feb00

countryb     01feb00

countryb     01mar00

countryb     01nov11

;

proc sort; by country date;

data want;

  set have;

  by country;

  if first.country or last.country;

run;

proc print; run;

coba
Calcite | Level 5

This is perfect! exactly what I needed.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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