Help using Base SAS procedures

retain variables for baseline

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

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
Respected Advisor
Posts: 3,156

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

View solution in original post


All Replies
Solution
‎04-18-2012 08:02 AM
Respected Advisor
Posts: 3,156

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. Smiley Happy

Respected Advisor
Posts: 3,799

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 261 views
  • 0 likes
  • 3 in conversation