BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ilikesas
Barite | Level 11

Hi,

suppose I have the following table:

stockdatecategorysize
A31/12/2010130
A31/12/2010020
A31/12/2010140
A27/11/201104
A27/11/201116
A27/11/201118

for each stock-date combination, I would like to get the total average size and the average size of category 1:

stockdatetotal average sizecat1 average size
A31/12/20103035
A27/11/201167

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards expandtabs truncover;
input stock $ date : ddmmyy10. cat size;
format date ddmmyy10.;
cards;
A     31/12/2010     1     30
A     31/12/2010     0     20
A     31/12/2010     1     40
A     27/11/2011     0     4
A     27/11/2011     1     6
A     27/11/2011     1     8
;
run;

proc sql;
create table want as
 select stock,date,mean(size) as mean_all,
        (select mean(size) from have where stock=a.stock and date=a.date and cat=1) as mean_1_cat
  from have as a
   group by stock,date;
quit;

Xia Keshan

View solution in original post

4 REPLIES 4
Reeza
Super User

Not quite the same format but try a proc means looking at WAYS and TYPE statement.

You could do a single proc sql as well.

samirt
Fluorite | Level 6

Code:-

proc sql;

     create table sample1 as

     select stock,date,  avg(size) as total_average_size

          from stock

     group by date;

quit;

proc sql;

    

create table sample2 as     

     select stock,date, category,avg(size) as cat1_avg_size  

          from stock

     where category=1

     group by date, category;

quit;

proc sql;

     create table final as

          select a.*, b.cat1_avg_size

          from sample1 a, sample2 b

          where a.stock=b.stock;

quit;

Steelers_In_DC
Barite | Level 11

data have;

infile cards dsd;

informat date ddmmyy10.;

format date ddmmyy10.;

input stock $ date category    size;

cards;

A,31/12/2010,1,30

A,31/12/2010,0,20

A,31/12/2010,1,40

A,27/11/2011,0,4

A,27/11/2011,1,6

A,27/11/2011,1,8

;

run;

proc sql;

create table want as

select distinct stock,avg_all,date,avg(size) as avg_one

from(

select distinct *,avg(size) as avg_all

from have

group by stock,date)

where category = 1

group by stock,date;

Ksharp
Super User
data have;
infile cards expandtabs truncover;
input stock $ date : ddmmyy10. cat size;
format date ddmmyy10.;
cards;
A     31/12/2010     1     30
A     31/12/2010     0     20
A     31/12/2010     1     40
A     27/11/2011     0     4
A     27/11/2011     1     6
A     27/11/2011     1     8
;
run;

proc sql;
create table want as
 select stock,date,mean(size) as mean_all,
        (select mean(size) from have where stock=a.stock and date=a.date and cat=1) as mean_1_cat
  from have as a
   group by stock,date;
quit;

Xia Keshan

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1600 views
  • 0 likes
  • 5 in conversation