filling empty variables conditional on non empty variables

Solved
Super Contributor
Posts: 459

filling empty variables conditional on non empty variables

Hi,

suppose i have the following table:

obspct sharespct stock
13070
240
355
4

The sum of the 2 pct shares and pct stock should equal to 100 for every observation. In the cases when one of the two is missing, I need to find the other one by subtracting the non missing value from 100. In the cases when both are present or missing leave as is.

So the desired table is the following:

obspct sharespct stock
13070
24060
34555
4

Thank you!

Accepted Solutions
Solution
‎05-10-2015 04:05 PM
Posts: 1,270

Re: filling empty variables conditional on non empty variables

data want;

set have;

if pctshares=. then pctshares=100-pctstock;

if pctstock=. then pctstock=100-pctshares;

run;

All Replies
Solution
‎05-10-2015 04:05 PM
Posts: 1,270

Re: filling empty variables conditional on non empty variables

data want;

set have;

if pctshares=. then pctshares=100-pctstock;

if pctstock=. then pctstock=100-pctshares;

run;

Posts: 4,692

Re: filling empty variables conditional on non empty variables

Amending the code from stat@sas to only calculate values "when one of the two is missing".

data want;

set have;

if cmiss(pctshares,pctstock)=1 then

do;

if pctshares=. then pctshares=100-pctstock;

else pctstock=100-pctshares;

end;

run;

🔒 This topic is solved and locked.