Hello,
I have data points collected at 3 dates (in SAS format) for each patient and need to select the first and third in order to find averages for the data point and perform a ttest. How would I do this?
I came across a similar question and saw people suggest a method using proc sql but will this just give me an output listing what the first/third dates are or will it reorganize the dataset? My instructor told me I need to create 2 files and merge them so do I create a file for the first date then one for the third and then merge them? Thanks for any help!
Here is a sample and as you can see, the dates are not ordered.
ID | ML | SF | date |
1 | 37 | 39 | 4/5/2013 |
1 | 24 | 29 | 2/26/2015 |
1 | 22 | 36 | 12/15/2012 |
3 | 68 | 100 | 12/31/2010 |
3 | 76 | 88 | 7/24/2013 |
3 | 68 | 86 | 12/17/2015 |
4 | 70 | 95 | 6/20/2013 |
4 | 71 | 95 | 4/14/2016 |
4 | 40 | 95 | 6/21/2011 |
Do you want to calculate the average value between first and third records? Then check this
data have;
input date mmddyy10. val;
datalines;
05/28/2018 10
05/29/2018 20
05/30/2018 30
;
run;
data want;
merge have have(keep=val firstobs=2 rename=(val=Val_));
lag_val=Lag(Val);
Avg=Val_+Lag_val/2;
run;
Sorry! I uploaded a sample just now
so even though the date arent in order SAS will identify first and last correctly? I'm looking to get the means for all variables at the first and third time point and compare the first and third SF score by way of a paired t test.
Sort by ID and DATE before you do the analysis.
Ordering of data is trivial, the main issue is understanding what format PROC TTEST would like for the data.
For a two sample t-test its a long format, for a paired t-test you need them separate or take the difference and use a single t-test on the difference.
You don't explain the 'averages' part and not sure how that relates to a t-test either. Here's an example to help you get started.
data have;
informat date mmddyy10.;
input ID ML SF date;
cards;
1 37 39 4/5/2013
1 24 29 2/26/2015
1 22 36 12/15/2012
3 68 100 12/31/2010
3 76 88 7/24/2013
3 68 86 12/17/2015
4 70 95 6/20/2013
4 71 95 4/14/2016
4 40 95 6/21/2011
;
run;
proc sort data=have;
by id date;
run;
data filtered;
set have;
by id date;
if first.id or last.id then do;
if first.id then count=1; else count=2;output;
end;
run;
proc ttest data=filtered;
class count;
var ml sf;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.