I have 80 patients measured on 200 variables at two timepoints. Two separate datasets were generated with identical IDs of patients and identical variables in the same order. I'm intrested in the effect between the two timepoints by substracting each variable from its corresponding in the other dataset.
I give examples of the datasets.
Thank you
data1:
id,C1,C2,C3,C4
1,4.10855,5.44574,33.16678,6.75791
3,3.48004,6.29138,31.34662,10.38753
4,2.33851,5.84293,35.79064,11.0801
9,3.1966,7.15718,30.27008,7.49836
data2:
id,C1,C2,C3,C4
1,4.19855,5.74574,33.46678,6.85391
3,3.48004,6.69138,31.85662,11.73753
4,3.33851,5.74293,36.09064,10.9801
9,3.2966,8.15718,30.27008,7.62836
Hi @Job04 , if you have IML licensed this is as simple as performing subtraction between two matrices, assuming as you say that you have the same IDs in the same order for both datasets. Simple code example below
*Read dummy data in;
data work.data1;
input id $ c1 c2 c3 c4 ;
datalines;
1 4.10855 5.44574 33.16678 6.75791
3 3.48004 6.29138 31.34662 10.38753
4 2.33851 5.84293 35.79064 11.0801
9 3.1966 7.15718 30.27008 7.49836
;
data work.data2;
input id $ c1 c2 c3 c4 ;
datalines;
1 4.19855 5.74574 33.46678 6.85391
3 3.48004 6.69138 31.85662 11.73753
4 3.33851 5.74293 36.09064 10.9801
9 3.2966 8.15718 30.27008 7.62836
;
*IML Procedure to load tables and perform matrix difference;
proc iml;
title 'Run IML Procedure';
*read data 1;
use work.data1;
read all var _NUM_ into A[colname=varNames];
print A;
*read data 2;
use work.data2;
read all var _NUM_ into B[colname=varNames];
print B;
*matrix difference;
diff = A-B;
print diff;
*Save output;
create work.diff from diff[colname=varNames];
append from diff;
close;
quit;
*Join IDs to output table;
data work.diff_out;
set work.data1(keep=id) ;
set work.diff;
run;
proc print data=work.diff_out;title 'Results as SAS Dataset with IDs';quit;
Output should look something like this:
Thanks
Harry
Hi @Job04 , if you have IML licensed this is as simple as performing subtraction between two matrices, assuming as you say that you have the same IDs in the same order for both datasets. Simple code example below
*Read dummy data in;
data work.data1;
input id $ c1 c2 c3 c4 ;
datalines;
1 4.10855 5.44574 33.16678 6.75791
3 3.48004 6.29138 31.34662 10.38753
4 2.33851 5.84293 35.79064 11.0801
9 3.1966 7.15718 30.27008 7.49836
;
data work.data2;
input id $ c1 c2 c3 c4 ;
datalines;
1 4.19855 5.74574 33.46678 6.85391
3 3.48004 6.69138 31.85662 11.73753
4 3.33851 5.74293 36.09064 10.9801
9 3.2966 8.15718 30.27008 7.62836
;
*IML Procedure to load tables and perform matrix difference;
proc iml;
title 'Run IML Procedure';
*read data 1;
use work.data1;
read all var _NUM_ into A[colname=varNames];
print A;
*read data 2;
use work.data2;
read all var _NUM_ into B[colname=varNames];
print B;
*matrix difference;
diff = A-B;
print diff;
*Save output;
create work.diff from diff[colname=varNames];
append from diff;
close;
quit;
*Join IDs to output table;
data work.diff_out;
set work.data1(keep=id) ;
set work.diff;
run;
proc print data=work.diff_out;title 'Results as SAS Dataset with IDs';quit;
Output should look something like this:
Thanks
Harry
If you don't have IML, try (assumes that both datasets are sorted by id)
data work.substract;
set work.data1 work.data2;
by id;
array vars c1-c4;
do i = 1 to dim(vars);
vars[i] = dif(vars[i]) * -1;
end;
if last.id;
drop i;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.