BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DavidPhillips2
Rhodochrosite | Level 12

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;

1 ACCEPTED SOLUTION

Accepted Solutions
11 REPLIES 11
Kurt_Bremser
Super User

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.

DavidPhillips2
Rhodochrosite | Level 12

This should solve it, currently testing..

data_null__
Jade | Level 19

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.

Astounding
PROC Star

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;

Kurt_Bremser
Super User

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.

DavidPhillips2
Rhodochrosite | Level 12

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.

Kurt_Bremser
Super User

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.

data_null__
Jade | Level 19

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

Kurt_Bremser
Super User

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.

data_null__
Jade | Level 19

I see no logic in your "reasoning".

 

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

Astounding
PROC Star

Not:

 

retain deg_cum=0;

 

Just:

 

deg_cum=0;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 11 replies
  • 2510 views
  • 8 likes
  • 4 in conversation