Hello everyone! As a practice exercise I've been writing a macro that does this: substitutes the n-th occurrence of a substring within a string with another string. Thus, if I have the string KingRobertKingGeorgeKingAdamKing and I will use the macro to substitute, say, the 2nd occurrence of King with, say, Queen, and haveKingRobertQueenGeorgeKingAdamKing. Now I've written a fairly successful macro to do this, except that it shows some behaviour it is not supposed to; here is the code (but don't read it line by line; just focus on where the problem arises, as explained in the red text and after the code). I provide the code as well as the data-set I use it with if you want to run it for yourself and see the issue first-hand. So... %macro auxm(ds, col, str1, str2, n_occurr); %let ds_id = %sysfunc(open(&ds)); %let colVarnum = %sysfunc(varnum(&ds_id, &col)); %let colVartype = %sysfunc(vartype(&ds_id, &colVarnum)); /* Check */ %put Check: vartype for the column is &colVartype; %if &colVartype ne C %then %put The column is not character; %else %do; %put The column is character; %let lstr1 = %length(&str1); /* Check */ %put Length of the string to be changed is &lstr1; data &ds.2; set &ds; temp = &col; if &n_occurr = 1 then do; if index(temp, "&str1") = 1 then temp = "&str2"||substr(temp, &lstr1 + 1); else temp = substr(temp, 1, index(temp, "&str1") - 1) || "&str2" || substr(temp, index(temp, "&str1") + &lstr1); end; else do; Problem arises here: the code in green below is not supposed to be hit when n_occurr = 1, but it does get hit! Why? do i = 1 to (&n_occurr - 1); if index(temp, "&str1") = 1 then temp = substr(temp, &lstr1 + 1); else temp = substr(temp, 1, index(temp, "&str1") - 1) || substr(temp, index(temp, "&str1") + &lstr1); end; drop i; pos = index(temp, "&str1") + (&n_occurr - 1)*(&lstr1); col3 = &col; col3 = substr(col3, 1, pos - 1)||"&str2"||substr(col3, pos + &lstr1); drop age temp pos; end; run; %end; %mend auxm; In more detail, my problem is this: since I work with two cases, based on whether n_occurr = 1 or n_occurr > 1 (i.e., whether I want to change the first or any subsequent occurrence of a given substring), when I have n_occurr = 1 for some reason my code also hits the else do condition. I test this code with the dataset data test; input Name $25. age; datalines; RadoslavRadossRado 12 IvanRadowwRadollRados 5 SimonRadoseRadosseqRadok 31 ; run; and with %auxm(test, Name, Rado, XXX, 1), which works as supposed only when I put in comments the else-do part above; or %auxm(test, Name, Rado, XXX, 2), which works fine. Any idea will be much appreciated!
... View more