I’m trying to use a simple retain with an array but the value deg_cum is producing a much larger number. The output should be something like 1, 8, 32, and 43. Am I missing something syntactical here?
retain deg_cum 0;
do i = 1 to dim(a);
deg_cum + d[i];
output;
end;
Simply do this:
deg_cum = 0;
do i = 1 to dim(a);
deg_cum + d[i];
output;
end;
I repeat: you do not need RETAIN at all, if I read your intentions right.
RETAIN is meant to keep values across data step iterations, not during DO loops.
So your deg_cum will grow with every observation of your input dataset.
Depending on your intention, I don't think you need the RETAIN at all.
This should solve it, currently testing..
If you're summing the array for each observation you don't want RETAIN DEG_CUM.
If you want to accumulate sums across observations for each array element you need and arrary of DEG_CUM1-DEC_CUMn that are also retained.
If you are just summing the values in ARRAY a for each obs you can use deg_cum=sum(of a[*]);
Or if you don't want me to keep guessing show some data.
An additional note about this syntax:
deg_cum + /* anything */;
This automatically retains deg_cum. If you continue to use this (and an alternative has already been suggested), you will need to change your original RETAIN statement to:
deg_cum=0;
Setting deg_cum to zero is alway a good idea, as it will be set to missing at the start of the current iteration of the data step.
deg_cum = deg_cum + ....; would then always result in another missing value, if deg_cum was not explicitly set to something else.
When I set
retain deg_cum= 0; I receive the error:
_
22
200
60 The SAS System 09:16 Tuesday, March 15, 2016
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,
a missing value, (, -, :, ;, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.
ERROR 200-322: The symbol is not recognized and will be ignored.
Simply do this:
deg_cum = 0;
do i = 1 to dim(a);
deg_cum + d[i];
output;
end;
I repeat: you do not need RETAIN at all, if I read your intentions right.
Nor DO I=1 to DIM(a); or any of it. I can all be done with SUM function and and OF variable-list.
You are very right, BUT:
The sum(of ...) construct is very specific to SAS and may be confusing to people coming from other backgrounds.
The DO loop and iterative cumulation is perfectly understandable for anybody coming from PASCAL, C, BASIC or any other procedural programming language.
That's why I like to use such common constructs more, especially around here where most readers are just starting into SAS.
I see no logic in your "reasoning".
Pretty sure the concept of a function is not beyond the comprehension of even the rankest beginner.
Not:
retain deg_cum=0;
Just:
deg_cum=0;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.