BookmarkSubscribeRSS Feed
rla_hoz
Calcite | Level 5

Dear SAS community,

 

I have two SAS files (Lung) that has one row per study subject. This first file has the baseline characteristics. The second SAS file  (Lung_followup) has the follow-up data for the study subjects. This second file may have zero or more rows per study subject. The number of rows is dependent on the number of years of follow-up the study subject has accrued. For example, those that do not have one year of follow-up do not have any rows in this second file while those that have 5 years of follow-up will have 5 rows. The second file has the rows in chronological order, e.g. the first row corresponds to the first year follow-up and the second to the second year follow-up.

 

I am trying to merge the two aforementioned SAS files in a single file (Lung_merge). My goal is to have a file that has one row per patient and contains the baseline characteristics of the first SAS file (Lung) and the 3 year follow-up data (third row of the second file). If the study subject doesn't have 3 year follow-up data I don't want to include it in the merged file (Lung_merge).

 

Could you please help me with the code? This is what I have so far and I am quite far from achieving my goal.

 

Data Lung_Merge;
Merge Lung (IN=in1) Lung_followup (IN=in2);
By TRR_ID_CODE;
Run;
 
Thank you for your advice. I use SAS 9.4
 
Best
2 REPLIES 2
jvdl
Obsidian | Level 7

It would help if you could show a data snippet, or at least provide some dummy data in a cards/datalines statement.

 

You've mentioned that for "Lung_followup" the row number for each subject corresponds to the follow-up year. Are you sure you can rely on this property to always be true?

 

In that case, you can probably count the row number per subject, and just keep where this counter equals 3. This code may need a bit of tweaking, I just did it off the top of my head.

 

proc sort data=lung_followup out=lf_sorted;
	by subjid;
run;

data lf_with_counter;
	set lf_sorted;
	by subjid;

	if first.subjid then counter = 1;
	else counter + 1;
run;

data Lung_Merge;
	merge Lung (IN=in1) lf_with_counter (IN=in2 where=(counter = 3));
	by subjid;

	if in1 and in2;
run;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 286 views
  • 0 likes
  • 3 in conversation