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

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

1 ACCEPTED SOLUTION

Accepted Solutions
HarrySnart
SAS Employee

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:

HarrySnart_0-1646307132763.png

Thanks

Harry

View solution in original post

2 REPLIES 2
HarrySnart
SAS Employee

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:

HarrySnart_0-1646307132763.png

Thanks

Harry

andreas_lds
Jade | Level 19

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 290 views
  • 2 likes
  • 3 in conversation