SWAP Subroutines

I thought it would be nice to write a little SAS subroutine that swaps the values of two variables—for example, a is assigned the contents of b and b is assigned the contents of a—without the need to worry about creating a temporary "holding" variable which subsequently needs to be dropped.  Swapping the contents of two variables is something we programmers do pretty often, so here is what I came up with.

First we need to define (and store) two subroutines that we will subsequently call from any SAS program that uses them.  SWAP_N will swap the values of two numeric variables, and SWAP_C will swap the values of two character variables.  In both cases, the two variables we are swapping will need to have the same LENGTH, otherwise truncation will occur.  You'll notice that a temporary variable h (short for "hold") is created within the subroutine definition, but it is local and will not be added to the calling DATA step nor change the value if you happen to have a DATA step variable called h.

*************************************************************************************************;
* Program Name: Create the SWAP_N and SWAP_C Subroutines, and Store Them in SASUSER for Permanent Access *;
* Last Updated: 12/02/14 by David Oesper                                                                                                                            *;
*************************************************************************************************;
proc fcmp outlib=sasuser.funcs.swap;
subroutine swap_n(a,b);
outargs a, b;
h = a;
a = b;
b = h;
endsub;
subroutine swap_c(a $,b $);
outargs a, b;
h = a;
a = b;
b = h;
endsub;
run;

Now, if you're going to replace a subroutine you've already stored in SASUSER, you might want to delete the deprecated version first.

**************************************************************************************;
* Program Name: How to Delete the Existing Version of SWAP_N and/or SWAP_C Prior to Replacement *;
* Last Updated: 12/02/14 by David Oesper                                                                                                      *;
**************************************************************************************;
proc fcmp outlib=sasuser.funcs.swap;
deletesubr swap_n;
deletesubr swap_c;
run;

Finally, here's a simple SAS DATA step showing you how to use both of these subroutines.

******************************************************;
* Program Name: Using the SWAP_N and SWAP_C Subroutines *;
* Last Updated: 12/02/14 by David Oesper                                      *;
******************************************************;
options cmplib=sasuser.funcs;

data _null_;

length a b $6; /* variables to be swapped must have the same length, otherwise truncation will occur */
x = 1;
y = 2;
a = 'Mickey';
b = 'Mouse';
put _all_;
call swap_n(x,y);
call swap_c(a,b);
put _all_;
run;

And here's the output this DATA _NULL_ step sends to the SAS log, showing us that the two user-defined subroutines work as advertised.

SAS log after invoking swap_n and swap_c