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

I need to calculate the change in a set of variables measured before and after an intervention. I have two data sets (pre and post). Each of the variable names are the same in each data set, and they are all paired by an ID variable. There are over 50 variables where I need to calculate the difference. 

 

I know I can merge the data sets and rename one "pre" or "post" then use a PAIRED statement in PROC TTEST to compare their means, or create a difference variable for individual comparisons. Since there are >50 variables to compare, is there a way to either batch-rename them, and then compare them/calculate differences in a merged data set, or some other way to compare the variables?

 

On SAS 9.4 (/STAT IML 14.3)

 

The data look something like this:

data pre;
input ID WT HT BMI SBP STRAIN EF ...;
datalines;
...
...
;
run;

data post;
input ID WT HT BMI SBP STRAIN EF ...;
datalines;
...
...
;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @webster1002 and welcome to the SAS Support Communities!

 

You could let PROC COMPARE calculate the differences and then perform a one-sample t-test on the differences, which is the same as a paired t-test:

proc compare data=pre c=post noprint out=diff(drop=_:);
var _numeric_;
id id;
run;

proc ttest data=diff;
var _numeric_;
run;

If not all numeric variables are to be compared, please adapt the variable list (_numeric_) to your needs. We can help you with this, too.

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Probably a macro loop would work changing WT to PRE_WT and so on.

 

%macro dothis;
    %let names=WT HT ... ;
    %let prestring=;
    %let poststring=;
    %do i=1 %to %sysfunc(countw(&names));
        %let thisname=%scan(&names,&i,%str( ));
        %let prestring=&prestring pre_&thisname;
        %let poststring=&poststring post_&thisname;
    %end;
    data pre;
        input id &prestring;
        ...
     run;
    data post;
        input id &poststring;
        ...
     run;
    data both;
        merge pre post;
        by id;
    run;
    proc ttest data=both;
        paired (&prestring):(&poststring);
    run;
%mend;
%dothis
        

 

--
Paige Miller
webster1002
Calcite | Level 5

I could be missing something, but can this be adapted if the two data sets already exist, since you cannot include datalines/cards in a macro?

PaigeMiller
Diamond | Level 26

@webster1002 wrote:

I could be missing something, but can this be adapted if the two data sets already exist, since you cannot include datalines/cards in a macro?


Yes, you could create customized RENAME statements in a SAS macro variable, using PROC CONTENTS and PROC SQL, or create customized RENAME statements in a loop as above; however, since @FreelanceReinh has come up with a very good solution, I'll stop here.

 

--
Paige Miller
FreelanceReinh
Jade | Level 19

Hello @webster1002 and welcome to the SAS Support Communities!

 

You could let PROC COMPARE calculate the differences and then perform a one-sample t-test on the differences, which is the same as a paired t-test:

proc compare data=pre c=post noprint out=diff(drop=_:);
var _numeric_;
id id;
run;

proc ttest data=diff;
var _numeric_;
run;

If not all numeric variables are to be compared, please adapt the variable list (_numeric_) to your needs. We can help you with this, too.

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
  • 1760 views
  • 2 likes
  • 3 in conversation