BookmarkSubscribeRSS Feed
lmyers2
Obsidian | Level 7

Hello,

 

I'm using SAS 9.4 full edition and am trying to add up money within an array. The variable MONEY below is coded as a numeric variable with format dollar15.2.  I think the if statement is the problem because I keep getting 0 as the answer. I don't know how to code that line which adds up all the amounts in the MONEY array for a given row.

 

Thanks for the help!

 

Best

Laura

 

array MONEY [50] MONEY1-MONEY50;
TotalCost=0;
do i= 1 to 50;
if MONEY [i]=' ' then TotalCost + ' ';
end;

format TotalCost dollar15.2;
2 REPLIES 2
mkeintz
PROC Star
This doesn't need a do loop.Instead you can:

array money {50} money1-money50;
totalcost=sum(of money{*});
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Reeza
Super User

Some pointers:

 

1. Space is missing for character variables not numeric. Use the MISSING() function for both numeric and character variables interchangeably.

2. The SUM operator (+) assigns the results to missing if any of the values are missing.

3. The SUM() function will treat missing as 0 or non existent. 

4. The SUM() function can take a list of variables or an array reference. 

 

All of this can be found in the relevant documentation. 

 

data demo;
a=0; b=.;
x = a+b;
y = sum(a, b);
run;

proc print data=demo;run;

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 721 views
  • 0 likes
  • 3 in conversation