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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2045 views
  • 2 likes
  • 3 in conversation