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

I started with two SAS data sets (reader + gold standard) with several body measurement values, added prefixes to differentiate which data set they came from, and merged them by ID to get something like:

 

data have;
 input ID $4. READER_ARM READER_LEG GOLD_ARM GOLD_LEG ;
 cards;
A123 5 8 2 6 9 2
B543 1 6 2 3 4 4
C859 4 7 2 5 8 4
;
run;

 

 

I'd like to calculate the difference and mean between variables with the same suffix to end up with:

data want;
 set have;

 DIFF_ARM=READER_ARM-GOLD_ARM;
 DIFF_LEG=READER_LEG-GOLD_LEG;

 MEAN_ARM=(READER_ARM+GOLD_ARM)/2;
 MEAN_LEG=(READER_LEG+GOLD_LEG)/2;

run;

Since my actual data set has many body measurement variables, how can I simplify this with an array or macro? I can rename the variables too if that makes it easier.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

If you know the list of suffixes in advance, you could easily write a macro to do this. (Do you know the list of suffixes in advance?)

 

UNTESTED CODE

 

%macro dothis(suffixes=);
    %let nsuffix=%sysfunc(countw(&suffixes));
    data want;
        set have;
        %do i=1 %to &nsuffix;
             %let thissuffix=%scan(&suffixes,&i,%str( ));
             diff_&thissuffix=sum(reader_&thissuffix,-gold_&thissuffix);
             mean_&thissuffix=mean(read_&thissuffix,gold_&thissuffix);
         %end;
    run;
%mend;
%dothis(suffixes=ARM LEG)
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

If you know the list of suffixes in advance, you could easily write a macro to do this. (Do you know the list of suffixes in advance?)

 

UNTESTED CODE

 

%macro dothis(suffixes=);
    %let nsuffix=%sysfunc(countw(&suffixes));
    data want;
        set have;
        %do i=1 %to &nsuffix;
             %let thissuffix=%scan(&suffixes,&i,%str( ));
             diff_&thissuffix=sum(reader_&thissuffix,-gold_&thissuffix);
             mean_&thissuffix=mean(read_&thissuffix,gold_&thissuffix);
         %end;
    run;
%mend;
%dothis(suffixes=ARM LEG)
--
Paige Miller
bkq32
Quartz | Level 8

Yes I do, and that worked perfectly. Thanks, Paige!

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 378 views
  • 0 likes
  • 2 in conversation