DATA Step, Macro, Functions and more

Selecting earliest/latest date for t-test

Reply
Contributor
Posts: 27

Selecting earliest/latest date for t-test

[ Edited ]

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.

 

IDMLSFdate
137394/5/2013
124292/26/2015
1223612/15/2012
36810012/31/2010
376887/24/2013
3688612/17/2015
470956/20/2013
471954/14/2016
440956/21/2011

 

 

 

Valued Guide
Posts: 593

Re: Selecting earliest/latest date for t-test

Posted in reply to iressa131

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;

 

 

Thanks,
Suryakiran
Super User
Posts: 23,727

Re: Selecting earliest/latest date for t-test

Posted in reply to iressa131
It depends on how your data is structured. For example, is it structured so that those 3 dates are all in one row or multiple rows. Please post some sample data, fake is fine, just needs to reflect your data structure.
Contributor
Posts: 27

Re: Selecting earliest/latest date for t-test

Sorry! I uploaded a sample just now

Super User
Posts: 23,727

Re: Selecting earliest/latest date for t-test

Posted in reply to iressa131
There are always more than one way to accomplish things.

1. Sort by ID
2. Use BY processing and FIRST/LAST to get the first and last record (or third).
3. Depends on what statistics you're wanting. If you provide that remaining detail we can help direct you on that stage as well.
Contributor
Posts: 27

Re: Selecting earliest/latest date for t-test

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. 

Respected Advisor
Posts: 3,022

Re: Selecting earliest/latest date for t-test

Posted in reply to iressa131

Sort by ID and DATE before you do the analysis. 

--
Paige Miller
Super User
Posts: 23,727

Re: Selecting earliest/latest date for t-test

Posted in reply to iressa131

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;


Ask a Question
Discussion stats
  • 7 replies
  • 109 views
  • 2 likes
  • 4 in conversation