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

Hi Sas Community,

 

I have the following dataset:

 

Date                      SEDOL                   Buy_sell               Buy_Vol              Sell_Vol

2007-08-17          440273                  10                           1200                       0

2007-08-17          440273                  10                           800                         0

2007-08-17          440273                  10                           1000                       0

2007-08-17          440273                  10                           800                         0

2007-08-17          440273                  10                           1200                       0

2007-08-17          440273                  20                           0                              200

2007-08-18          450576                  20                           0                              3000

2007-08-18          450576                  20                           0                              5000

2007-08-18          450576                  20                           0                              2000

2007-08-19          450576                  10                           800                         0

 

and i want to do the following:

 

Date                      SEDOL         Buy_Vol            Sell_Vol       No of_buy_order       No of_sell_order

2007-08-17          440273         5000                      200                        5                                      1

2007-08-18          450576         0                            10000                    0                                      3

2007-08-19          450576         800                         800                       1                                      0

 

Any help will be appreciated. Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

When I run

data have;
input Date :yymmdd10.                     SEDOL                   Buy_sell               Buy_Vol              Sell_Vol;
cards;
2007-08-17          440273                  10                           1200                       0
2007-08-17          440273                  10                           800                         0
2007-08-17          440273                  10                           1000                       0
2007-08-17          440273                  10                           800                         0
2007-08-17          440273                  10                           1200                       0
2007-08-17          440273                  20                           0                              200
2007-08-18          450576                  20                           0                              3000
2007-08-18          450576                  20                           0                              5000
2007-08-18          450576                  20                           0                              2000
2007-08-19          450576                  10                           800                         0
;
run;

proc sql;
create table want as
select
  date,
  sedol,
  sum(buy_vol) as buy_vol,
  sum(sell_vol) as sell_vol,
  sum(case
    when buy_vol ne 0 then 1
    else 0
  end) as no_of_buy_order,
  sum(case
    when sell_vol ne 0 then 1
    else 0
  end) as no_of_sell_order
from have
group by date, sedol;
quit;

proc print data=want noobs;
format date yymmddd10.;
run;

I get

                                                            no_of_
                                                 no_of_      sell_
      Date     SEDOL    buy_vol    sell_vol    buy_order     order

2007-08-17    440273      5000         200         5           1  
2007-08-18    450576         0       10000         0           3  
2007-08-19    450576       800           0         1           0  

View solution in original post

9 REPLIES 9
Kurt_Bremser
Super User

"Community Matters" is for issues concerning the working of the SAS Communities. I moved your question to the proper SAS Programming community.

 

Try this

proc sql;
create table want as
select
  date,
  sedol,
  sum(buy_vol) as buy_vol,
  sum(sell_vol) as sell_vol,
  sum(case
    when buy_vol be 0 then 1
    else 0
  end) as no_of_buy_order
  sum(case
    when sell_vol ne 0 then 1
    else 0
  end) as no_of_sell_order
from have
group by date, sedol;
quit;
bd_user_10
Quartz | Level 8
Thanks Kurt! there are few small issues in the codes, e.g.' 'be' needs to be 'ne', before the last sum there needs to be a ','. However, for some reason no_of_buy_order and no_of_sell_order are exactly the same in my new table. I cannot figure out why that is the case. Am i missing anything?
Kurt_Bremser
Super User

When I run

data have;
input Date :yymmdd10.                     SEDOL                   Buy_sell               Buy_Vol              Sell_Vol;
cards;
2007-08-17          440273                  10                           1200                       0
2007-08-17          440273                  10                           800                         0
2007-08-17          440273                  10                           1000                       0
2007-08-17          440273                  10                           800                         0
2007-08-17          440273                  10                           1200                       0
2007-08-17          440273                  20                           0                              200
2007-08-18          450576                  20                           0                              3000
2007-08-18          450576                  20                           0                              5000
2007-08-18          450576                  20                           0                              2000
2007-08-19          450576                  10                           800                         0
;
run;

proc sql;
create table want as
select
  date,
  sedol,
  sum(buy_vol) as buy_vol,
  sum(sell_vol) as sell_vol,
  sum(case
    when buy_vol ne 0 then 1
    else 0
  end) as no_of_buy_order,
  sum(case
    when sell_vol ne 0 then 1
    else 0
  end) as no_of_sell_order
from have
group by date, sedol;
quit;

proc print data=want noobs;
format date yymmddd10.;
run;

I get

                                                            no_of_
                                                 no_of_      sell_
      Date     SEDOL    buy_vol    sell_vol    buy_order     order

2007-08-17    440273      5000         200         5           1  
2007-08-18    450576         0       10000         0           3  
2007-08-19    450576       800           0         1           0  
PaigeMiller
Diamond | Level 26

@bd_user_10 wrote:
However, for some reason no_of_buy_order and no_of_sell_order are exactly the same in my new table. I cannot figure out why that is the case. Am i missing anything?

If your code isn't giving the same answer as @Kurt_Bremser gets, then you have changed something somehow and the only way we can help you further is if you show us the code you are using.

 

As with your other question, this can be done via PROC SUMMARY as well.

--
Paige Miller
bd_user_10
Quartz | Level 8

Here is the dataset:

 

ObsDateSEDOLPRCCDBUY_SELLVOLUMEPRICEBuy_volumeSell_volume
11996-01-22431813070.....
21996-01-25431813070.....
31996-01-26431813073.....
41996-01-29431813072.....
51996-01-304318130731020073200.
61996-01-31431813075.9101900751900.
71996-01-31431813075.920500075.5000
81996-01-31431813075.92020075.9.200
91996-02-01431813080201000079.10000
101996-02-014318130801040077400.
111996-02-01431813080105000785000.
121996-02-0143181308010300007930000.
131996-02-014318130801090077900.
141996-02-02431813080101600801600.
151996-02-05431813085201000082.10000
161996-02-05431813085202250082.22500
171996-02-06431813085.....
181996-02-07431813085.....
191996-02-09431813084.....
201996-02-13431813084.....
211996-02-14431813084.....
221996-02-15431813084.....
231996-02-16431813084.....
241996-02-2043181308510200008520000.
251996-02-2143181308310100008310000.
261996-02-2143181308310960008396000.
271996-02-2243181308510500085.55000.
281996-02-23431813085.5.....
291996-02-28431813086.....
301996-02-2943181308520100085.1000
311996-02-2943181308520100085.1000

 

This is the code I used

 

proc sql;

create table want_2 as

select

date,

sedol'

sum(Buy_volume) as Buy_volume,

sum(Sell_volume) as Sell_volume,

sum(case

when Buy_volume ne 0 then 1

else 0

end) as no_of_buy_order,

sum(case

when Sell_volume ne 0 then 1

else 0

end) as no_of_sell_order

from Have

group by date, sedol;

quit;

 

This is what I get

 

ObsDateSEDOLBuy_volumeSell_volumeno_of_buy_orderno_of_sell_order
11996-01-224318130..11
21996-01-254318130..11
31996-01-264318130..11
41996-01-294318130..11
51996-01-304318130200.11
61996-01-3143181301900520033
71996-02-014318130363001000055
81996-02-0243181301600.11
91996-02-054318130.3250022
101996-02-064318130..11
111996-02-074318130..11
121996-02-094318130..11
131996-02-134318130..11
141996-02-144318130..11
151996-02-154318130..11
161996-02-164318130..11
171996-02-20431813020000.11
181996-02-214318130106000.22
191996-02-2243181305000.11
201996-02-234318130..11
211996-02-284318130..11
221996-02-294318130.200022
231996-03-044318130..11
241996-03-054318130..11
251996-03-074318130..11
261996-03-134318130..11
271996-03-144318130..11
281996-03-154318130..11
291996-03-204318130..11
301996-03-254318130..11
311996-03-294318130200.11
PaigeMiller
Diamond | Level 26

You have changed the data from your example. We (including @Kurt_Bremser) can only give you code that works on your example; we can't give you code that works on other data sets that you haven't shown us.

 

In particular, you now have missing values which your original data set did not have. So ... homework assignment ... modify Kurt's code to account for missings.

--
Paige Miller
bd_user_10
Quartz | Level 8
Thank you for your hint. That worked!
ballardw
Super User

And another approach with a different procedure which is intended to work with missing values instead of 0 for the buy sell. Though the variable names have also apparently changed...

proc tabulate data=have;
   class date sedol;
   var buy_vol sell_vol;
   table date='' * sedol='',
         buy_vol='Buy Volume'*sum=''*f=best10.
         sell_vol='Sell Volume'*sum=''*f=best10.
         (buy_vol='Number buy orders' sell_vol='Number sell orders')*n=''
         /misstext='0'
   ;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 1307 views
  • 0 likes
  • 4 in conversation