retain variables for baseline

Solved
Super Contributor
Posts: 301

retain variables for baseline

Hi everyone, i got the next dataset:

visit       phase

baseline   1

week2      2

week4      2

week6      1

baseline   2

week2      1

week4      1

week6      1

I want something like this

visit        phase   visit_b     phase_b

baseline   1       baseline    1

week2      2     baseline    1

week4       2    baseline    1

week6      1     baseline    1

baseline   2     baseline    2

week2      1    baseline     2

week4      1    baseline     2

week6      1    baseline     2

Any help...maybe with the retain function...thanks in advance.

V

Accepted Solutions
Solution
‎04-18-2012 08:02 AM
Posts: 3,167

Re: retain variables for baseline

Yes, you can use retain:

data have;

input (visit phase) (:\$);

cards;

baseline 1

week2 2

week4 2

week6 1

baseline 2

week2 1

week4 1

week6 1

;

data want;

set have;

if visit='baseline' then do;

visit_b=visit;

phase_b=phase;

end;

retain visit_b phase_b;

run;

Or not: (variables from table are automatically retained)

data want;

if 0 then set have (rename=(phase=phase_b visit=visit_b));

set have;

if visit='baseline' then do;

visit_b=visit;

phase_b=phase;

end;

run;

Or not using retain and silly: (temporary array are automatically retained)

data want;

set have;

array _v(2) \$ _temporary_;

array _b \$ visit_b phase_b;

if visit='baseline' then do;

_v(1)=visit;

_v(2)=phase;

end;

do _n_=1 to dim(_b);

_b(_n_)=_v(_n_);

end;

run;

Haikuo

All Replies
Solution
‎04-18-2012 08:02 AM
Posts: 3,167

Re: retain variables for baseline

Yes, you can use retain:

data have;

input (visit phase) (:\$);

cards;

baseline 1

week2 2

week4 2

week6 1

baseline 2

week2 1

week4 1

week6 1

;

data want;

set have;

if visit='baseline' then do;

visit_b=visit;

phase_b=phase;

end;

retain visit_b phase_b;

run;

Or not: (variables from table are automatically retained)

data want;

if 0 then set have (rename=(phase=phase_b visit=visit_b));

set have;

if visit='baseline' then do;

visit_b=visit;

phase_b=phase;

end;

run;

Or not using retain and silly: (temporary array are automatically retained)

data want;

set have;

array _v(2) \$ _temporary_;

array _b \$ visit_b phase_b;

if visit='baseline' then do;

_v(1)=visit;

_v(2)=phase;

end;

do _n_=1 to dim(_b);

_b(_n_)=_v(_n_);

end;

run;

Haikuo

Super Contributor
Posts: 301

Re: retain variables for baseline

Thanks Hai, I will use the first one with retain.

Posts: 3,852

Re: retain variables for baseline

Since you will surely have SUBJECTS or other grouping you will want INIT TO MISSING when a new subject is started to handle the possibility that a subject has no baseline.

🔒 This topic is solved and locked.