## porcentage in a dataset

Solved
Super Contributor
Posts: 301

# porcentage in a dataset

Hi guys, I would like you to help me with this:

I got the next dataset:

text                     drug  placebo

randomized          100     101

treated                 100     100

completed            90       85

discontinued         10      16

I would like to built this dataset:

test                     drug                  placebo

randomized            100                  101

treated                   100 (100/100)  100(101/100)

completed              90  (90/100)      85(85/101)

discontinued           10(10/100)        16(16/101)

As you can see, my idea is to keep the first record of the dataset, 100 and 101 and assigned a variable to these as r1=100 and r2=101,

then write down something like:

drug=drug||' ' || (drug/r1);

placebo=placebo||''||(placebo/r2)

I know is very simplistic,  for thsi reason  I would like you to help me to write a piece of code maybe using call symput or symget to create the macrovariables r1 and r2,,

or maybe usinf the retain option to keep the first record.

J V

Accepted Solutions
Solution
‎03-15-2012 08:36 PM
Super Contributor
Posts: 301

## porcentage in a dataset

Thanks, it works.

Example:

data new;

set sashelp.class (keep=name age);

age2=age*2;

run;

data new2 (drop=age_n age2_n age age2);

length agen \$20 age2n \$20;

set new;

retain age_n age2_n;

if _n_=1 then do;

age_n=age;

age2_n=age2;

agen=put(age,3.);

age2n=put(age2,3.);

end;

else do;

agen=put(age,3.)||' '||'('||put((age/age_n)*100,6.2)||')';

age2n=put(age2,3.)||' '||'('||put((age2/age2_n)*100,6.2)||')';

end;

run;

All Replies
Super User
Posts: 23,724

## porcentage in a dataset

You don't need macro variables you need retained variables within your dataset.

data have;

set want;

retain placebo_n drug_n;

if _n_=1 then do;

placebo_n = placebo;

drug_n=drug;

new_placebo=placebo;

new_drug=drug;

end;

else do;

drug2=drug|| " (" ||drug"/" blah blah;

placebo2=placebo2 || ...blah blah;

end;

run;

Solution
‎03-15-2012 08:36 PM
Super Contributor
Posts: 301

## porcentage in a dataset

Thanks, it works.

Example:

data new;

set sashelp.class (keep=name age);

age2=age*2;

run;

data new2 (drop=age_n age2_n age age2);

length agen \$20 age2n \$20;

set new;

retain age_n age2_n;

if _n_=1 then do;

age_n=age;

age2_n=age2;

agen=put(age,3.);

age2n=put(age2,3.);

end;

else do;

agen=put(age,3.)||' '||'('||put((age/age_n)*100,6.2)||')';

age2n=put(age2,3.)||' '||'('||put((age2/age2_n)*100,6.2)||')';

end;

run;

🔒 This topic is solved and locked.