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;
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;
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:);
@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[*]);
@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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.