BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jing2000yr
Calcite | Level 5

Hello,everyone,

I am a green hand of SAS,and I appreciate very much your kindly help.

My data is as below,each subject has multiple visit.Time is the time length compared to the last visit,total time is the time length compared to the first visit.Stage=0 is no disease and stage=1 is diagnosed with disease.I want to compute the disease free time.For example,for the first patient,the disease free time(variable:length) is 13,since he was first diagnosed at the third visit,and by that time,13 months passed.For the second subject,should be 8 months since he was diagnosed at the second visit ,so at the third visit,the disease free time should still be 8 months.But my output is not correctly for the situation like the second subject.

Thanks!

  Obs    id      treatment      visit    time    stage    totalm

      1     1    control group      1        0       0         0
      2     1    control group      2        4       0         4
      3     1    control group      3        9       1        13
      4     2    control group      1        0       0         0
      5     2    control group      2        8       1         8
      6     2    control group      3        4       1        12
      7     3    control group      1        0       0         0
      8     3    control group      2        5       0         5
      9     3    control group      3        5       0        10
     10     3    control group      4        9       0        19
     11     4    control group      1        0       0         0

coding:

data hw9;

set hw9;

by id visit;

retain length;

if first.id then length=0;

if stage=1 then length=totalm;

/*if stage=1 then length=length;*/

if last.id and stage=0 then length=totalm;

/*if last.id then output;*/

run;

proc print data=hw9;

run;

output:


  Obs   id     treatment     visit   time   stage   totalm   length

    1    1   control group     1       0      0        0        0
    2    1   control group     2       4      0        4        0
    3    1   control group     3       9      1       13       13
    4    2   control group     1       0      0        0        0
    5    2   control group     2       8      1        8        8
    6    2   control group     3       4      1       12       12
    7    3   control group     1       0      0        0        0
    8    3   control group     2       5      0        5        0
    9    3   control group     3       5      0       10        0
   10    3   control group     4       9      0       19       19

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Is the following what you are trying to accomplish?:

data hw9;

  informat treatment $20.;

  input  id   treatment &     visit    time    stage    totalm;

  cards;

1    control group      1        0       0         0

1    control group      2        4       0         4

1    control group      3        9       1        13

2    control group      1        0       0         0

2    control group      2        8       1         8

2    control group      3        4       1        12

3    control group      1        0       0         0

3    control group      2        5       0         5

3    control group      3        5       0        10

3    control group      4        9       0        19

4    control group      1        0       0         0

;

data hw9;

  set hw9;

  by id visit;

  retain length start;

  if first.id then do;

    length=0;

    start=0;

  end;

  if stage=1 and start=0 then do;

    length=totalm;

    start=1;

  end;

  /*if stage=1 then length=length;*/

  if last.id and stage=0 then length=totalm;

  /*if last.id then output;*/

run;

proc print data=hw9;

run;

View solution in original post

7 REPLIES 7
art297
Opal | Level 21

Is the following what you are trying to accomplish?:

data hw9;

  informat treatment $20.;

  input  id   treatment &     visit    time    stage    totalm;

  cards;

1    control group      1        0       0         0

1    control group      2        4       0         4

1    control group      3        9       1        13

2    control group      1        0       0         0

2    control group      2        8       1         8

2    control group      3        4       1        12

3    control group      1        0       0         0

3    control group      2        5       0         5

3    control group      3        5       0        10

3    control group      4        9       0        19

4    control group      1        0       0         0

;

data hw9;

  set hw9;

  by id visit;

  retain length start;

  if first.id then do;

    length=0;

    start=0;

  end;

  if stage=1 and start=0 then do;

    length=totalm;

    start=1;

  end;

  /*if stage=1 then length=length;*/

  if last.id and stage=0 then length=totalm;

  /*if last.id then output;*/

run;

proc print data=hw9;

run;

jing2000yr
Calcite | Level 5

No.I want to add a new variable length which represent disease free time length,like the lower part 'output' in which the variable length is not computed correctly for subject id 2,since he got the disease at the second visit,the disease free time should be 8 months.Then his third visit,the disease free time should still be 8 months,but my coding output 12 months.

The variable time is the time length compared the former visit,like visit2 compared to visit 1,and visit 3 compared to visit 2.The totalm is the time length compared to the first visit,like visit 2 compare to 1,visit 3 compared to 1.

Thank you!

jing2000yr
Calcite | Level 5

Dear Arthur,

Oh...Thank you so so much!

It works!!!

Jing

art297
Opal | Level 21

Jing: Then mark the question as answered so that others won't spend time trying to come up with a solution.

jing2000yr
Calcite | Level 5

Dear Arthur,

It is my first time entering this forum.I did not see the tab with which I can mark the question as answered.Sorry,I am trying.

art297
Opal | Level 21

If you click on my response there will be an icon there that you can click indicating that it is a correct answer.

Nothing to be sorry about but, if one doesn't take that action, a number of people will still try to provide an answer.

jing2000yr
Calcite | Level 5

Thank you,Arthur.It is weird that I can not do this in the Windows system set up in virtual box in my Mac.But when I open the website in Mac itself,I saw the obvious tab to mark the right answer...

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1238 views
  • 1 like
  • 2 in conversation