DATA Step, Macro, Functions and more

Retain with Arrays

Accepted Solution Solved
Reply
Super Contributor
Posts: 477
Accepted Solution

Retain with Arrays

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;


Accepted Solutions
Solution
‎03-15-2016 10:06 AM
Super User
Posts: 6,926

Re: Retain with Arrays

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,926

Re: Retain with Arrays

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 477

Re: Retain with Arrays

This should solve it, currently testing..

Respected Advisor
Posts: 3,777

Re: Retain with Arrays

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.

Super User
Posts: 5,069

Re: Retain with Arrays

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;

Super User
Posts: 6,926

Re: Retain with Arrays

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 477

Re: Retain with Arrays

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.

Solution
‎03-15-2016 10:06 AM
Super User
Posts: 6,926

Re: Retain with Arrays

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,777

Re: Retain with Arrays

Nor DO I=1 to DIM(a); or any of it.  I can all be done with SUM function and and OF variable-list.

Super User
Posts: 6,926

Re: Retain with Arrays

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,777

Re: Retain with Arrays

I see no logic in your "reasoning".

 

Pretty sure the concept of a function is not beyond the comprehension of even the rankest beginner.

Super User
Posts: 5,069

Re: Retain with Arrays

Not:

 

retain deg_cum=0;

 

Just:

 

deg_cum=0;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 548 views
  • 8 likes
  • 4 in conversation