DATA Step, Macro, Functions and more

Compute Net Holdings

Occasional Contributor
Posts: 9

Compute Net Holdings

I am a newbie to SAS and am trying to figure out the best way to solve this problem. I have a data set of option holdings:
Name Action Quantity Maturity
Joe Buy 100 Dec 12
Joe Buy 100 Dec 10
Joe Sell 50 Dec 10
Mary Buy 50 Dec 12

I want the result to have net current holdings:
Joe 100 Dec 12
Joe 50 Dec 10
Mary 50 Dec 12

Is there a clean and elegant way to write this?

Super User
Posts: 9,687

Re: Compute Net Holdings

You can use ' proc sort nodup; by name descending maturity;run;' to get it.
SAS Employee
Posts: 27

Re: Compute Net Holdings

You could try something like this:

/* create a sample data set*/
data holdings;
informat maturity date9.;
format maturity date9.;
input name $ action $ quantity maturity;
Joe Buy 100 12-Dec-2010
Joe Buy 100 10-Dec-2010
Joe Sell 50 10-Dec-2010
Mary Buy 50 12-Dec-2010

/* for Sell transactions, change the sign on the quantity to negative */
data holdings2;
set holdings;
if action = "Sell" then do;
quantity = -quantity;

/* Group the transactions by name and maturity date */
proc sort data=holdings2;
by name maturity;

/* Add up the transactions for each group */
proc means data=holdings2 noprint;
by name maturity;
var quantity;
output out=holdings3 sum=;

/* Print results */
proc print data=holdings3 noobs;
var name maturity quantity;
Super Contributor
Super Contributor
Posts: 365

Re: Compute Net Holdings

Hello Sid S,

If I correctly understood what you need, the following code produces the result:

data a;
input Name $ 1-4 Action $ 6-10 Quantity 11-13 Maturity $ 15-20;
Joe Buy 100 Dec 12
Joe Buy 100 Dec 10
Joe Sell 50 Dec 10
Mary Buy 50 Dec 12
/* Add minus sign to quantity for activity=sell */;
data a1;
set a;
if UPCASE(Action)="SELL" then quantity=-quantity;
/* Summarizing Quantity by Name and Maturity */;
proc SQL;
create table r as
select Name, SUM(quantity) as Quantity, Maturity
from a1
group by Name, Maturity

Occasional Contributor
Posts: 9

Re: Compute Net Holdings

Thanks for your help. It really makes it easier to see this implementation using groups. Unfortunately my brain still think in C and that doesn't seem like the right approach for SAS.
BTW what is the advantage/disadvantage of using PROC SQL?
Posts: 8,744

Re: Compute Net Holdings

This is one of those apples/oranges questions. It depends on what you're accessing data-wise, how big the tables are, whether you're joining or summarizing or doing sub-queries, etc, etc. You really have to benchmark. Do you want to optimize for I/O operations; for CPU; for ease of maintenance???

As a "for-instance", if you need to get both matches and non-matches out of a single join/merge operation, then you can do that in a DATA step program with one program, whereas with SQL you need one join to get the matches and a second join to get the non-matches.

For a shop that has programmers with a lot of SQL expertise, then PROC SQL is a good entree into using SAS. If a shop has a lot of legacy files (such as sequential mainframe files and/or database files) to read, join, and process, then "regular" DATA step programming might be a better alternative. It all depends.

If you search for user group papers on PROC SQL and DATA step, you will find lots of papers that talk about the pros and cons to PROC SQL versus other SAS approaches.

Frequent Contributor
Posts: 94

Re: Compute Net Holdings

I am hoping this is a simple question.  I followed the advice on this thread to change a row of numbers from + to -.  Would you know what is it I am missing on line 47?

44         Data ab_2;

45         Set ab;

46         if AccountType="total saved" then do;

47         _4Q14=_-2014Q4;



ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN,

              LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=. 

Ask a Question
Discussion stats
  • 6 replies
  • 6 in conversation