DATA Step, Macro, Functions and more

multiply and subtract in retain function

Reply
Occasional Contributor CFF
Occasional Contributor
Posts: 11

multiply and subtract in retain function

Hi

I have 1 variabel A. I need to create a new variabel B thats i created through the retain function. For the first observation must B must be equal to 1.

For all the other values B=1-B*A - where B is the retained result.

As an example:

Data have:

A:

0,5

0,5

0,5

Data want

A:       B

0,5     1

0,5     0,5 (calculated as: 1-1*0,5)

0,5     0,75 (calculated as: 1-0,5*0,5)

Best regards

Super User
Posts: 10,500

Re: multiply and subtract in retain function

data want;

     set have;

     retain b 1;

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

run;

Occasional Contributor
Posts: 17

Re: multiply and subtract in retain function

DATA A10Aug2015_1;

  retain B 1;

  INPUT A;

  OUTPUT;

  B=1-B*A;

DATALINES;

0.5

0.5

0.5

;

Super User
Super User
Posts: 7,401

Re: multiply and subtract in retain function

I am not clear on what the dataset A is for, it doesn't add anything to the mix.  Adjust the do loop for number of iterations:

data want (drop=i);

  retain b 1;

  do i=1 to 5;

    b=1 - (b * 0.5);

    output;

  end;

run;

Ask a Question
Discussion stats
  • 3 replies
  • 291 views
  • 0 likes
  • 4 in conversation