Coding with longitudinal data

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Coding with longitudinal data

So I have a dataset and I need to find a way to tell SAS to look at people measured in the first period who were smokers and then look at people in the second period and tell me whether they quit or are still smokers. The data originally looked like this:

Obs id CURSMOKE PERIOD 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

244801
244803
623801
623802
623803
942811
942812
1055211
1055212
1125211
1125212
1125213
1126301
1126302
1126303

 

I tried combining the periods and smoking status into one variable but I'm not sure if this is what I want to do, or if there's a more effective method of getting what I need.

 

Obs id smoker1 smoker2 smoker3

2448000
2448000
6238000
6238000
6238000
9428100
9428010
10552100
10552010
11252100
11252010
11252001
11263000
11263000
11263000

 

I need SAS to be able to look across individual IDs in the matrix and tell me if they are smokers in each period or if they quit, and I really don't know how  to do this. I tried making an array for it, but I would need to somehow integrate an if then statement into the array, which I couldn't find anything on how to do. Here's the start of my code, but I don't know how to finish it, or even if an array is what I want to do for this.

 

data smoket;
    set FRM;
    by id;
    
    keep smoke1 - smoke3;
    retain smoke1 - smoke3;
    
    array asmoke(3) smoke1-smoke3;
    
    if first.id then do;
    do i=1-3;
    asmoke(i) = .;
    end;
    
end;

 


asmoke(period)=cursmoke;
if last.id then output;
run;

 

Help please!!


Accepted Solutions
Solution
‎09-28-2015 09:44 PM
Respected Advisor
Posts: 4,606

Re: Coding with longitudinal data

I suggest a coding where each obsID is represented by a single observation. Then all is required is a transposition of your data... with a twist to account for missing intervening statuses.

 

data have;
input ObsID curSmoke period;
datalines;
2448	0	1
2448	0	3
6238	0	1
6238	0	2
6238	0	3
9428	1	1
9428	1	2
10552	1	1
10552	1	2
11252	1	1
11252	1	3
11263	0	1
11263	0	2
11263	0	3
;

proc sort data=have; by obsID period; run;

/* Fill in missing intervening periods. Assume last known status persists. */
data temp;
time = 1e40;
do until(last.obsID);
    set have; by obsID;
    time + 1;
    do while (time < period);
        output;
        time + 1;
        end; 
    time = period;
    smoke = curSmoke;
    output;
    end;
run;

proc transpose data=temp out=want( drop=_: ) prefix=smoker;
by obsID;
id time;
var smoke;
run;

proc print data=want noobs; run;

 

 

PG

View solution in original post


All Replies
Solution
‎09-28-2015 09:44 PM
Respected Advisor
Posts: 4,606

Re: Coding with longitudinal data

I suggest a coding where each obsID is represented by a single observation. Then all is required is a transposition of your data... with a twist to account for missing intervening statuses.

 

data have;
input ObsID curSmoke period;
datalines;
2448	0	1
2448	0	3
6238	0	1
6238	0	2
6238	0	3
9428	1	1
9428	1	2
10552	1	1
10552	1	2
11252	1	1
11252	1	3
11263	0	1
11263	0	2
11263	0	3
;

proc sort data=have; by obsID period; run;

/* Fill in missing intervening periods. Assume last known status persists. */
data temp;
time = 1e40;
do until(last.obsID);
    set have; by obsID;
    time + 1;
    do while (time < period);
        output;
        time + 1;
        end; 
    time = period;
    smoke = curSmoke;
    output;
    end;
run;

proc transpose data=temp out=want( drop=_: ) prefix=smoker;
by obsID;
id time;
var smoke;
run;

proc print data=want noobs; run;

 

 

PG
New Contributor
Posts: 2

Re: Coding with longitudinal data

Thank you!! I never could have gotten this on my own! Smiley Happy

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 224 views
  • 1 like
  • 2 in conversation