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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Anotherdream
Quartz | Level 8

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;

View solution in original post

6 REPLIES 6
ballardw
Super User

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.

CFF
Calcite | Level 5 CFF
Calcite | Level 5

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

Anotherdream
Quartz | Level 8

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;

Tom
Super User Tom
Super User

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;

CFF
Calcite | Level 5 CFF
Calcite | Level 5

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?

ballardw
Super User

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.

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
  • 6 replies
  • 2241 views
  • 4 likes
  • 4 in conversation