Sorting data by an ID variable over multiple date ranges?

Reply
Contributor
Posts: 21

Sorting data by an ID variable over multiple date ranges?

I have the following data:

ID           Date                     Weight

G3          01MAY2007          150

G1          12APR2007          200

C9          25MAR2007          175

A5          29APR2007          143

C9          12MAY2008          239

B6          31MAR2008          149

G3          09APR2008          148

G1          23APR2008          215

B6          11APR2009          175

G1          31MAY2009          202

C9          05MAR2009          200

L5          19APR2009           207

I wish to sort this data by ID number within each year range (i.e 01Jan200X-31DEC200X). Sorting this data only by ID will put all of the same ID's together reagdless of date. I would like to keep the IDs separated by date. Over the years some ID numbers will drop out of the dataset and new IDs will be added.

Thanks very much for any suggestions Smiley Happy

Super Contributor
Posts: 1,636

Re: Sorting data by an ID variable over multiple date ranges?

Is this helpful?

data have;

input ID :$2.          Date  : date9.                  Weight;

year=year(date);

format date date9.;

cards;

G1          12APR2007          200

C9          25MAR2007          175

A5          29APR2007          143

C9          12MAY2008          239

B6          31MAR2008          149

G3          09APR2008          148

G1          23APR2008          215

B6          11APR2009          175

G1          31MAY2009          202

C9          05MAR2009          200

L5          19APR2009           207

;

proc sort data=have out=want;

by year id;

run;

proc print data=want;run;

Contributor
Posts: 21

Re: Sorting data by an ID variable over multiple date ranges?

Thanks! That worked perfectly.

Contributor
Posts: 21

Re: Sorting data by an ID variable over multiple date ranges?

One further question. If certain cowIDs are missing dates and weights for a certain how do I sort this data. For example:

ID           Date                     Weight

G3          01MAY2007          150

G1        

C9          25MAR2007          175

A5          29APR2007          143

C9          12MAY2008          239

B6          31MAR2008          149

G3          09APR2008          148

G1          23APR2008          215

B6          11APR2009          175

G1          31MAY2009          202

C9        

L5          19APR2009           207


I would like to keep these CowIDs with no data sorted within the years they were not taken.

Thanks!

Super Contributor
Posts: 1,636

Re: Sorting data by an ID variable over multiple date ranges?

data have;

infile cards missover;

input ID :$2.          Date  : date9.                  Weight;

year=year(date);

year=ifn(year=.,lag(year),year);

format date date9.;

cards;

G3          01MAY2007          150

G1       

C9          25MAR2007          175

A5          29APR2007          143

C9          12MAY2008          239

B6          31MAR2008          149

G3          09APR2008          148

G1          23APR2008          215

B6          11APR2009          175

G1          31MAY2009          202

C9       

L5          19APR2009           207

;

proc sort data=have out=want;

by year id;

run;

proc print data=want;run;

Ask a Question
Discussion stats
  • 4 replies
  • 255 views
  • 0 likes
  • 2 in conversation