It's a bit late for me, so the brain is turning to mush here. But here's an approach that should work.
If you have a six-letter word, you will always want the second letter. So ...
Set up an array with 20 elements, all zero.
Apply some numeric algorithm that just escapes me for now. But after 1 iteration, it is 6 and thus turns the sixth array element to 1.
Then repeat. On the second iteration, the mystery algorithm generates 1, and thus turns the first array element to 1.
On the third iteration, turn the third array element to 1.
The point is that you don't need to change the character string. You can work with the positions of the elements, and track which ones have already been changed. When the number of changed elements is one less than the number of letters in the word, the first zero is the position of the letter that remains.
%let string=DONALD; data _null_; c="&string"; p=length(c); do i=1 to p-1; substr(c,mod(p-1,length(c))+1,1)=' '; c=compress(c); put c; end; run;
Edit: Not surprisingly, the sequence of the last remaining positions (of initial strings of length 1, 2, 3, ...) can be found in the OEIS: http://oeis.org/A128982.
Wow. Didn't know there was such a thing as OEIS. You not only knew about it, but knew how to find the right problem.
Maybe this is my fault for looking at this before the morning cup of coffee, but is that formula correct? Isn't it looking for the 7th character on the first iteration?
Actually, I first wrote my program and then suspected that there might be a corresponding entry in the OEIS -- given that the resulting sequence of integers is both non-trivial and easily defined.
On the first iteration in the example the character at position mod(p-1,length(c))+1=mod(6-1,6)+1=5+1=6 is replaced with a blank, as it should.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.