BookmarkSubscribeRSS Feed
RandyStan
Fluorite | Level 6

Dear All:

My data set is as follows:

 

Time                  p                  ID

9:00:01              56.57           45

9:00:01              67.97           46

9:00:01              67.97           46

9:00:01               68.00          46

9:00:02              69.00            76

9:00:02              69.10            79

9:00:02              70.01            45

9:00:03              69.04            78

9:00:03              68.10            46

 

and so on

 

The output I want is the p observation.  So in this example I need:

 

Time                  p                  ID

9:00:01              56.57           45

9:00:01              67.97           46

9:00:01               68.00          46

9:00:02              69.00            76

9:00:02              69.10            79

9:00:02              56.57            45

9:00:03              69.04            78

9:00:03              68.10            46

 

the code I wrote was

data want; set have;

by ID p time:

If first.p then output;

run;

 

Some thing does not seem to work with the code.

 

Thanks in advance.

R

5 REPLIES 5
Tom
Super User Tom
Super User

Perhaps your values are not actually exactly the same?

 

novinosrin
Tourmaline | Level 20
data have;
input Time  :time8.                p                  ID;
format time time8.;
cards;
9:00:01              56.57           45
9:00:01              67.97           46
9:00:01              67.97           46
9:00:01               68.00          46
9:00:02              69.00            76
9:00:02              69.10            79
9:00:02              70.01            45
9:00:03              69.04            78
9:00:03              68.10            4
;
proc sort data=have out=_have;
by id p time;
run;
data want; set _have;

by ID p time;

If first.p then output;

run;
Astounding
PROC Star
What result would you want if the order of p over time is

68
70
68

Should 68 appear twice?

It is possible that this program provides the proper result without sorting

Data want;
Set have;
By p notsorted;
If first.p;
Run;
mkeintz
PROC Star

Your sample data appears to be sorted by TIME/P/ID.  if that's true, then your idea of a data step with a BY statement and a"first." condition is correct, but needs tweaking, assuming what you want is to eliminate duplicate time/p/id combinations.

 

data want;
  set have;
  by time p id;
  if first.id;
run;

Of course this means a given id  or a given P can appears more than once for a given TIME.  I presume that's ok.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
SuryaKiran
Meteorite | Level 14

Hello,

 

Are you trying to remove duplicate records, try the nodupkey option.

 

proc sort data=have out=want nodupkey;
by id p time;
run;
Thanks,
Suryakiran

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1207 views
  • 0 likes
  • 6 in conversation