Is there any information on how to create one observation per subject between the first and last visit in SAS data?
Thanks,
@michelle05 wrote:
Is there any information on how to create one observation per subject between the first and last visit in SAS data?
Much more information is needed. Specifically, we need to see a portion of your data AND an example of the desired output from this sample data.
So I am to create a new data set (CHANGE) with one observation per subject with the difference in WEIGHT between the first and last visit. I am also to include in this data set the number of days between the first and last visit. Do not include any patients who have only one visit.
This is what I have done so far.
DATA CLINICAL;
*Use LENGTH statement to control the order of
variables in the data set;
LENGTH PATIENT VISIT DATE_VISIT 8;
RETAIN DATE_VISIT WEIGHT;
DO PATIENT = 1 TO 25;
IF RANUNI(135) LT .5 THEN GENDER = 'FEMALE';
ELSE GENDER = 'MALE';
X = RANUNI (135);
IF X LT .33 THEN GROUP = 'A';
ELSE IF X LT .66 THEN GROUP = 'B';
ELSE GROUP = 'C';
DO VISIT = 1 TO INT (RANUNI(135)*5);
IF VISIT = 1 THEN DO;
DATE_VISIT = INT(RANUNI(135)*100)+15800;
WEIGHT = INT(RANNOR(135)*10 + 150);
END;
ELSE DO;
DATE_VISIT = DATE_VISIT + VISIT*(10 + INT(RANUNI(135)*50));
WEIGHT= WEIGHT + INT(RANNOR(135)*10);
END;
OUTPUT;
IF RANUNI(135) LT .2 THEN LEAVE;
END;
END;
DROP X;
FORMAT DATE_VISIT DATE9.;
RUN;
*Assume data set Clinical is already sorted by VISIT and DATE;
DATA DIFFERENCE;
SET CLINICAL;
LENGTH;
DIFF_WEIGHT= WEIGHT-LAG(WEIGHT);
IF NOT FIRST.VISIT THEN OUTPUT;
PROC PRINT DATA=DIFFERENCE;
RUN;
DATA CHANGE;
SET CLINICAL;
DIFF_WEIGHT=WEIGHT-LAG(WEIGHT);
***OMIT PATIENT WITH ONLY ONE VISIT;
IF FIRST.VISIT AND LAST.VISIT THEN DELETE;
***IF IT IS THE FIRST VISIT ASSIGN VALUES TO THE
RETAINED VARIABLES;
We need to see a portion of your data AND an example of the desired output from this sample data.
Are you asking about after I run the code?
This is the portion that I entered in.
*Assume data set Clinical is already sorted by VISIT and DATE;
DATA DIFFERENCE;
SET CLINICAL;
LENGTH;
DIFF_WEIGHT= WEIGHT-LAG(WEIGHT);
IF NOT FIRST.VISIT THEN OUTPUT;
PROC PRINT DATA=DIFFERENCE;
RUN;
DATA CHANGE;
SET CLINICAL;
DIFF_WEIGHT=WEIGHT-LAG(WEIGHT);
***OMIT PATIENT WITH ONLY ONE VISIT;
IF FIRST.VISIT AND LAST.VISIT THEN DELETE;
***IF IT IS THE FIRST VISIT ASSIGN VALUES TO THE
RETAINED VARIABLES;
It's really hard to know what's not working if we don't see the data. Ideally provide sample data in form of a SAS data step and then show us how the desired result should look like.
Looking into your code you've got the following Note:
NOTE: Variable 'FIRST.VISIT'n is uninitialized.
What this means: You forgot to add a BY VISIT DATE; to your data step. For this reason your IF FIRST.VISIT logic is not going to work.
Also: Please do me the favour and end a SAS Data Step with a RUN; statement;
Below the code you've posted with the BY and RUN statements added.
*Assume data set Clinical is already sorted by VISIT and DATE;
DATA DIFFERENCE;
SET CLINICAL;
by visit date;
LENGTH;
DIFF_WEIGHT= WEIGHT-LAG(WEIGHT);
IF NOT FIRST.VISIT THEN
OUTPUT;
run;
PROC PRINT DATA=DIFFERENCE;
RUN;
DATA CHANGE;
SET CLINICAL;
by visit date;
DIFF_WEIGHT=WEIGHT-LAG(WEIGHT);
***OMIT PATIENT WITH ONLY ONE VISIT;
IF FIRST.VISIT AND LAST.VISIT THEN
DELETE;
***IF IT IS THE FIRST VISIT ASSIGN VALUES TO THE
RETAINED VARIABLES;
run;
Yes sir! I promise I am trying my best to learn the language and programming. I get myself confused to the point where I'm always confused and doubting myself. Thank you for the information.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.