Help using Base SAS procedures

choosing the first date and last date in a dataset

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

choosing the first date and last date in a dataset

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


Accepted Solutions
Solution
‎12-12-2011 11:20 PM
Super Contributor
Posts: 1,636

choosing the first date and last date in a dataset

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


All Replies
PROC Star
Posts: 7,486

choosing the first date and last date in a dataset

Change two lines in your code:

  by ID;

  if first.ID or last.ID then output;


Solution
‎12-12-2011 11:20 PM
Super Contributor
Posts: 1,636

choosing the first date and last date in a dataset

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;

Contributor
Posts: 32

choosing the first date and last date in a dataset

This is perfect! exactly what I needed.

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 150 views
  • 0 likes
  • 3 in conversation