DATA Step, Macro, Functions and more

Collapsing dataset while keeping other variables

Reply
Occasional Contributor
Posts: 6

Collapsing dataset while keeping other variables

Hello there,

 

I want to collapse (group) my data by time (seconds). Sounds easy, well, but doesn't work.

I assume it is because CEIL does not round to integers but that's just a guess and I tried INT without success.

 

Why do I still get duplicate seconds observations? (The volume variable should be summed up when collapsing.)

So basically: I converted milliseconds to seconds and want to bring them to unique seconds observations. So, keep all observations, collapse seconds (which are duplicate) and sum up their values in the variable "volume". 

Any idea?

 

 

data temp;
set have;
seconds = ceil(int(miliseconds/1000));
run;

proc sql; 
  create table want as
  	select *, sum(volume) as SecVolume
  	from temp
		group by seconds;
quit;run;

proc sort data=want;
by id date seconds;
run;

 

Super User
Posts: 6,936

Re: Collapsing dataset while keeping other variables

[ Edited ]

Take a close look at the log of your SQL; you'll find a NOTE about remerging summary statistics.

If you include columns other than the summary values and the columns used in the group by, all observations will appear in the output, and the summary results will be merged back into all of them.

Your SQL should look rather like this:

proc sql; 
  create table want as
  	select id, date, seconds, sum(volume) as SecVolume
  	from temp
		group by id, date, seconds;
quit;run;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 7

Re: Collapsing dataset while keeping other variables

Works perfect, thanks again Kurt Smiley Happy

Super User
Posts: 10,500

Re: Collapsing dataset while keeping other variables

I obviously don't know where you are going with this but I would suggest creating a SAS time valued variable. Then you can use a FORMAT that only displays to whole seconds (if I understand what your attempt at rounding is doing) for most procedures.

 


data example;
   input time;
datalines;
1.1
1.2
0.003
5.02
5.04
120.23
120.45
;
run;

proc freq data=example;
   tables time;
   format time time8.;
run;
Occasional Contributor
Posts: 7

Re: Collapsing dataset while keeping other variables

Thank you, solved it with proc sql but freq will also work!

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