BookmarkSubscribeRSS Feed
SidS
Calcite | Level 5
Hello,
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?

Thanks,
Sid
6 REPLIES 6
Ksharp
Super User
Hi.
You can use ' proc sort nodup; by name descending maturity;run;' to get it.
Daryl
SAS Employee
You could try something like this:

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

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

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

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

/* Print results */
proc print data=holdings3 noobs;
var name maturity quantity;
run;
[/pre]
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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;
datalines;
Joe Buy 100 Dec 12
Joe Buy 100 Dec 10
Joe Sell 50 Dec 10
Mary Buy 50 Dec 12
;
run;
/* Add minus sign to quantity for activity=sell */;
data a1;
set a;
if UPCASE(Action)="SELL" then quantity=-quantity;
run;
/* 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
;quit;

Sincerely,
SPR
SidS
Calcite | Level 5
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?
Cynthia_sas
SAS Super FREQ
Hi:
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.

cynthia
jen123
Fluorite | Level 6

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;

                        __

                        22

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

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1131 views
  • 0 likes
  • 6 in conversation