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

I have the following issue in SAS:  Consider the table below:

Patient_IDDayLipid_LevelsFirst_Obs
00010

10.2

10.2
0001111.610.2
0001214.510.2
0001310.510.2
000209.99.9
0002110.79.9
0002211.49.9
0002311.19.9
0003014.314.3
0003117.214.3
0003214.114.3
0003314.514.3

I have a dataset named "indata" with the variable Patient_ID, Day, and Lipid_Level, and I need to create a new variable First_Obs that is equal to the first observation for a given patient ( i.e. the Lipid Level when the Day-within-Patient_ID value is equal to zero).

Again, I have the first three columns, and I need to create the fourth.  Any suggestions??

Thank you for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
SKK
Calcite | Level 5 SKK
Calcite | Level 5

Hi there are several methods to do BOCF...Try this..

PROC SORT DATA=indata;
BY PATIENT_ID DAY;
RUN;

DATA WANT;
SET indata;
RETAIN FIRST_OBS;
BY PATIENT_ID DAY;
IF FIRST.PATIENT_ID AND FIRST.DAY THEN FIRST_OBS=LIPID_LEVELS;
RUN;

View solution in original post

5 REPLIES 5
SKK
Calcite | Level 5 SKK
Calcite | Level 5

Hi there are several methods to do BOCF...Try this..

PROC SORT DATA=indata;
BY PATIENT_ID DAY;
RUN;

DATA WANT;
SET indata;
RETAIN FIRST_OBS;
BY PATIENT_ID DAY;
IF FIRST.PATIENT_ID AND FIRST.DAY THEN FIRST_OBS=LIPID_LEVELS;
RUN;

ballardw
Super User

Probably don't want the First.day as it will reset for each unique value of day.

SKK
Calcite | Level 5 SKK
Calcite | Level 5

Try this:

PROC SQL;

CREATE TABLE WANT AS

SELECT A.PATIENT_ID, DAY , LIPID, FIRST_OBS  FROM HAVE AS A INNER JOIN

(SELECT PATIENT_ID, LIPID AS FIRST_OBS FROM HAVE GROUP BY PATIENT_ID HAVING DAY=MIN(DAY)) AS B

ON A.PATIENT_ID=B.PATIENT_ID;

QUIT;

LukeKlein
Calcite | Level 5

Thank you very much! 

I used the following code to achieve what I needed, then found your response:

proc sort data = indata;

by Pateint_ID day

run;

data want;

  set indata;

  by Patient_ID;

  retain First_Obs;

  if first.Patient_ID then FirstObs=Lipid_Levels;

run;quit;

Kurt_Bremser
Super User

Does the patient ID change if the same person is admitted repeatedly?

If not, you may need to include the actual date in the data set.

Then try the following:

proc sort data=HAVE;

by PATIENT_ID DATE;

run;

data WANT;

set HAVE;

retain FIRST_OBS;

if DAY = 0 then FIRST_OBS=LIPID_LEVELS;

run;

If, instead, patients get a new PATIENT_ID on each admission, change DATE in the sort to DAY;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1085 views
  • 0 likes
  • 4 in conversation