BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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
Meteorite | Level 14

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 563 views
  • 1 like
  • 4 in conversation