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
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
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
Thanks Hai, I will use the first one with retain.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.