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
Cheers.
data answer(drop=_b);
set base;
retain b_;
if _n_=1 then do;
b=1;
b_=b;
end;
else do;
b=a*b_;
b_=b;
end;
run;
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.
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
Cheers.
data answer(drop=_b);
set base;
retain b_;
if _n_=1 then do;
b=1;
b_=b;
end;
else do;
b=a*b_;
b_=b;
end;
run;
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;
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?
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.
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.