data have;
str='JOHN';
run;
data want;
set have;
do until(lengthn(str)=1);
substr(str,lengthn(str),1)=' ';
output;
end;
run;
str |
---|
JOH |
JO |
J |
Must it be length of 4 as a start?
Then
data have;
str='JOHNhhk';
run;
data want;
set have;
ln=4;
do until(ln=1);
substr(str,ln)=' ';
output;
ln=lengthn(str);
end;
run;
proc print noobs;run;
str | ln |
---|---|
JOH | 4 |
JO | 3 |
J | 2 |
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.
Hi @sneha_dodle,
Try this:
%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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.