BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to sum up all variables start with "x"

Way1 to do it  is:

x_sum1=sum(x1901,x1902,x1903,x1904,x1905,x1906);

Since in real I have many fields then I want to find a more clever way to do it

Why is it not working?

x_sum2=sum(x:);

_
388
200
76
ERROR 388-185: Expecting an arithmetic operator.

ERROR 200-322: The symbol is not recognized and will be ignored.

ERROR 76-322: Syntax error, statement will be ignored.

 

data a;
input x1901 x1902 x1903 x1904 x1905 x1906;
cards;
10 20 30 40 50 60
;
run;

data b;
set a;
x_sum1=sum(x1901,x1902,x1903,x1904,x1905,x1906);
x_sum2=sum(x:);
run;
4 REPLIES 4
Ronein
Onyx | Level 15

I found the solution.

Is there another way to do it?

Maybe with arrays?

 

data b;
set a;
sum1=sum(x1901,x1902,x1903,x1904,x1905,x1906);
sum2=sum(of  x:);
run;

 

andreas_lds
Jade | Level 19

Why do you think that using arrays will allow an easier solution? I don't know a solution with less code than

sum2=sum(of x:);
Tom
Super User Tom
Super User

@Ronein wrote:

I found the solution.

Is there another way to do it?

Maybe with arrays?

 

data b;
set a;
sum1=sum(x1901,x1902,x1903,x1904,x1905,x1906);
sum2=sum(of  x:);
run;

 


If you already have the array defined then you can use the array name with * for the index.  So if you defined the array using the name Xarray the syntax would look like:

sum3=sum(of  Xarray[*]);
ballardw
Super User

@Ronein wrote:

Hello

I want to sum up all variables start with "x"

Way1 to do it  is:

x_sum1=sum(x1901,x1902,x1903,x1904,x1905,x1906);

Since in real I have many fields then I want to find a more clever way to do it

Why is it not working?

x_sum2=sum(x:);

_
388
200
76
ERROR 388-185: Expecting an arithmetic operator.

ERROR 200-322: The symbol is not recognized and will be ignored.

ERROR 76-322: Syntax error, statement will be ignored.

 

data a;
input x1901 x1902 x1903 x1904 x1905 x1906;
cards;
10 20 30 40 50 60
;
run;

data b;
set a;
x_sum1=sum(x1901,x1902,x1903,x1904,x1905,x1906);
x_sum2=sum(x:);
run;

Reiterate: Paste LOG into a code box as well.

When you use a list indicator like X: then you need the key word "of" as in sum(of x: ) ; Otherwise the compiler doesn't know what you want to do. Similar with an array as the argument. Error: z= sum ( y(*) );  Correct: z= sum (of y(*)); (Assumes y is a correctly defined array)

BTW if you use

x_sum1=sum(x1901,x1902,x1903,x1904,x1905,x1906);
x_sum2=sum( of x:);

then x_sum2 will be equivalent to sum(x_sum1,x1901,x1902,x1903,x1904,x1905,x1906); assuming that x1901 - x1906 were all of the other X variables. So you may want to be careful with the order of your code and variable name lists.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 1183 views
  • 1 like
  • 4 in conversation