## Using retain an multiply down a collumn

Solved
Occasional Contributor
Posts: 11

# Using retain an multiply down a collumn

Hi

I have column A and B, where as the first obs in A is set to 0 and the first obs in B is set to 1.

I would like to multiply down column B, using retain to that i multiply A*B, where the column B always will have the result from the retain value*value of column A.

My code is:

if _n_=1 then A=0 and B=1;

data check;

set example;

retain b;

b=b*a;

run;

But that does not seem to work?

Best regards

Accepted Solutions
Solution
‎08-07-2015 12:58 PM
Super Contributor
Posts: 418

## Re: Using retain an multiply down a collumn

Cheers.

set base;

retain b_;

if _n_=1 then do;

b=1;

b_=b;

end;

else do;

b=a*b_;

b_=b;

end;

run;

All Replies
Super User
Posts: 13,583

## Re: Using retain an multiply down a collumn

First describe how it "does not seem to work".

It helps determine what you are trying to do if you can provide some example input data and the desired output.

Does your example data set have a variable B? That will cause issues.

Also b=b*a; Would change the value of B for each record.

You likely want something that looks like:

bmult = b*a;   Adding a NEW variable to keep the result. But note that if you have B in the example data set you're still likely no to get the results you expect.

Occasional Contributor
Posts: 11

## Re: Using retain an multiply down a collumn

Thanks for the quick response!

What I have is 2 variabels A and B.

Variabel A has all its values, but I want to create variabel B through the function retain. I want the first observation to have the value 1.

The rest of the values i want to create as retained value from B and multiply it with the value from A.

Numeric Example:

What I have (I have created numbers that are easy to illustrate my point):

Variabel A:       Variabel B:

0                          .

0.5                       .

0.5                        .

0.5

0.5

What I want then is the retained value to be multiplied with the A Variabel.

Variabel A:      Variabel B:

0                              1

0.5                         0.5

0.5                         0.25

16                         4

0,25                       1

etc

I Hope this makes sense

Solution
‎08-07-2015 12:58 PM
Super Contributor
Posts: 418

## Re: Using retain an multiply down a collumn

Cheers.

set base;

retain b_;

if _n_=1 then do;

b=1;

b_=b;

end;

else do;

b=a*b_;

b_=b;

end;

run;

Super User
Posts: 8,127

## Re: Using retain an multiply down a collumn

You do NOT want to already have B on the INPUT dataset, otherwise the RETAIN will not work since the retained value will be overwritten when a new record is read.

data have ; input a @@; cards;

0 0.5 0.5 16 0.25

;;;;

data want ;

set have ;

retain b 1;

if _n_ > 1 then b=b*a ;

run;

Occasional Contributor
Posts: 11

## Re: Using retain an multiply down a collumn

Perfect! Thanks. That helped me a lot.

But I have a new question:

What if I instead having b=b*a, but b=1-b*a in the retain function?

If i replaced in the code:

data want ;

set have ;

retain b 1;

if _n_ > 1 then b=1-b*a ;

run;

Then it does not work. What do I do?

Super User
Posts: 13,583

## Re: Using retain an multiply down a collumn

Likely to be the same issue: B is in the data set HAVE and overwrites any retained value.

Also it is a good idea to describe how some code does not work: no output at all, error message, unexpected output.

If error message then provide the error message; best is copy code and error from the log. If the output is unexpected provide the actual output and the expected output.

🔒 This topic is solved and locked.

Discussion stats
• 6 replies
• 610 views
• 4 likes
• 4 in conversation