BookmarkSubscribeRSS Feed
iressa131
Calcite | Level 5

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

 

 

 

7 REPLIES 7
SuryaKiran
Meteorite | Level 14

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
Reeza
Super User
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.
iressa131
Calcite | Level 5

Sorry! I uploaded a sample just now

Reeza
Super User
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.
iressa131
Calcite | Level 5

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. 

PaigeMiller
Diamond | Level 26

Sort by ID and DATE before you do the analysis. 

--
Paige Miller
Reeza
Super User

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;


sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 845 views
  • 2 likes
  • 4 in conversation