BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14
Hello
I want to calculate all numeric vars along a row.
Is there a way to tell sas to calculate in each row(observation)
Sum of all numeric values?
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input var1 var2 var3;
cards;
1 2 3
4 5 6
7 8 9
;
data want;
set have;
sum=sum(of _numeric_);
run;

you mean this?

 

data want;

set have;

sum=sum(of _numeric_);

run;

 

View solution in original post

5 REPLIES 5
Ronein
Meteorite | Level 14
The issue is that I have a dynamic program that run every month.
Number of fields are increasing every month.
So actually i want to calculate sum along row with non fix number of fields.


novinosrin
Tourmaline | Level 20
data have;
input var1 var2 var3;
cards;
1 2 3
4 5 6
7 8 9
;
data want;
set have;
sum=sum(of _numeric_);
run;

you mean this?

 

data want;

set have;

sum=sum(of _numeric_);

run;

 

ballardw
Super User

@Ronein wrote:
The issue is that I have a dynamic program that run every month.
Number of fields are increasing every month.
So actually i want to calculate sum along row with non fix number of fields.



That means your data model is wrong. As I believe has been mentioned a time or two.

andreas_lds
Jade | Level 19

If you can't use _numeric_ as suggested by @novinosrin - because you have numeric vars that should not be part of the calculation - then all variables need a common prefix, so that you can use

sum = sum(of prefix:)

.

 

But fixing the data-structure as recommended by @ballardw is the best way to solve the issue.

ballardw
Super User

@andreas_lds wrote:

If you can't use _numeric_ as suggested by @novinosrin - because you have numeric vars that should not be part of the calculation - then all variables need a common prefix, so that you can use

sum = sum(of prefix:)

.

 

But fixing the data-structure as recommended by @ballardw is the best way to solve the issue.


or if you have several "prefixes"

sum = sum( of prefix: , of other: , of different: );

or if all of the variables of interest are sequential in column order

sum = sum( firstvar -- lastvar); There are two -   in case your browser makes it hard to read.

 

But for stuff like this providing an example layout of your data and indicating which variables you wanted to sum does not seem unrealistic to ask.

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
  • 5 replies
  • 869 views
  • 2 likes
  • 4 in conversation