@dane77221: I like the elegance of the solution @ChrisNZ has offered. But since you said you want to specify nothing but the names of the surrounding variables, I'd offer one where you need to name only the variable after which you want the insertion (and, of course, necessarily the name and other attributes of the variable you want to insert). Thus, if you originally have a data set: data have ;
retain A "A" B "BB" C "CCC" E "EEEEE" F "FFFFFF" G 1 ;
do G = 1 to 10 ;
output ;
end ;
run ; and want to insert a variable D after variable C, you could code as follows: %let v_aft = C ;
%let v_ins = D ;
%let v_len = $4 ; * if you want D as numeric, just code 8 instead of $4 ;
data _null_ ;
do until (z) ;
set sashelp.vcolumn end = z ;
where libname = "WORK" and memname = "HAVE" ;
length v_list $ 32767 ;
v_list = catx (" ", v_list, name) ;
if name = "&v_aft" then v_list = catx (" ", v_list, "&v_ins") ;
end ;
call symputx ("v_list", v_list) ;
run ;
data want ;
retain &v_list ;
length &v_ins &v_len ;
call missing (&v_ins) ;
set have ;
run ; Now if you look at the data set WANT, you'll see the variable D right between C and E. The piece producing V_LIST may look rather maladroit, yet in reality it exacts little overhead since it only reads the metadata. Paul D.
... View more