BookmarkSubscribeRSS Feed
Xinxin
Obsidian | Level 7

When I use this code:

proc sql; create table finn_counts1 as select *, sum(finn36) as ct_finn36 from finn1
group by ACCOUNT_ID; quit;

 

I get the correct sum (albeit it's for each row of ACCOUNT_ID, and I can dedup that) but why does the order of the rows for each ACCOUNT_ID change? It's not sorted by any variable, looks randomly changed.

 

Get these notes in the log:

NOTE: The query requires remerging summary statistics back with the original
data.
NOTE: SAS threaded sort was used.

 

Do I need to sort the data again to keep the original sequence per ACCOUNT_ID ?

4 REPLIES 4
novinosrin
Tourmaline | Level 20

add on order by

 

order by account_id;

 

if you had used -

proc sql; 
create table finn_counts1 as
 select ACCOUNT_ID, sum(finn36) as ct_finn36
from finn1 
group by ACCOUNT_ID
order by ACCOUNT_ID;
 quit;

Proc sql wouldn't have remerged but I am assuming you perhaps want other variables besides the sum

novinosrin
Tourmaline | Level 20

Alternatively look at proc means, summary or just a one pass of the datastep to have better control of the order 

kiranv_
Rhodochrosite | Level 12

run both.

 

Intially dataset is ordersed by name. Once you want to group by sex all same gender are sorted so that summing of same becomes easy. this is part of underhood mechanism which you can understand by using _method

 

 

proc sql;
create table want as 
select * from sashelp.class;


proc sql;
create table want1 as 
select *, sum(age) from sashelp.class
group by sex;



 

 

 

/* then run _method*/


proc sql _method;
create table want1 as 
select *, sum(age) from sashelp.class
group by sex;

_method gives the following in log explaining how a group by is conducted.

 

sqxcrta
sqxsumg
sqxsort
sqxsrc( SASHELP.CLASS )

now the sort happens  on gender as per the _method.

sqxceta -- creating final dataset

sqxsumg - sum within group

sqxsort-- sorting( done on your group by variable)

sqxsrc -source table

 

To havefinal dataset as per you please try using @novinosrin suggestion. Then dataset will be reordered as per your needs

 
Xinxin
Obsidian | Level 7

Wow...thanks...learnt something new!
Yes, I ended up ordering by ACCOUNT_ID and some other variables.
Thanks to both of you ,@kiranv_ and @novinosrin !

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 808 views
  • 0 likes
  • 3 in conversation