%macro cinsert(str, /* name of a character variable to be edited by inserting a character string somewhere within it */ istr, /* identifies the string to be inserted. Usually a literal but it can be a char. variable as well, in which case it must be enclosed in angle brackets (< >) */ at, /* the starting location for the insertion. Do not specify a value longer than the length of &str . */ len, /* the length of the string to be inserted. If not specified then the length will be that of the literal specified at &istr or the trimmed value of the variable specified as &istr. */ debug=0); %*--utility macro that generates run time assignment stmts that result in the literal string &istr to be inserted within &str starting at position @at for a length of &len. if &str is enclosed in angle brackets (< >) then it is assumed to be a reference to a character variable rather than a literal. If it is a literal it cannot contain any double quotes. jgb, 11-10-04 --*; %if &at eq %str() %then %do; %put ****Invalid parameters passed to cinsert macro. The 3rd positional parm is missing.; %goto endmac; %end; %if %qsubstr(&istr,1,1)=%str(<) %then %do; %let istr=%substr(&istr,2,%eval(%length(&istr) -2)); %let _istrvar=1; %end; %else %do; %let istr="&istr"; %let _istrvar=0; %end; %if &len=0 or &len=%str() %then %do; %let len= length(trim(&istr)) ; %end; %if &debug %then %put istr edited value: &istr; %if &at ne 1 %then %let part1= substr(&str,1,%eval(&at-1))|| ; %else %let part1=%str(); if &at ge length(&str) then &str = &part1 &istr; else &str= &part1 trim(&istr) || substr(&str, &at); %endmac: %mend cinsert; /* Sample Invocations data a; s='And now we have what? '; x='X'; %cinsert(s,DOO,5,debug=1) put s=; %cinsert(s,,2,debug=1) put s=; %cinsert(s,S,1,debug=1) put s=; run; */