This question came in for @SASJedi and me on Twitter:
You have a character var with the string "000112010302". What's the least about of [SAS] code that can be written to determine what is the highest number (3) in the string?
I have an answer that works, but I'm certain it isn't the least amount of code possible. I figured that I'd post to the community and learn something.
Chris
How about this?
%sysfunc(findc(123456789,000112010302,b));
Or, in a data step:
data;
c='000112010302';
h=findc('123456789',c,-9);
run;
(Edit: replaced 'b' by -9 in the data step to save one more character)
Edit 2: At the cost of readability, one could replace '123456789' with 1e10/81-1 (two characters less).
Editor's note: this was the earliest, best solution that came in -- so it's the Accepted Solution. But it's worth reading through ALL of the responses to learn about different approaches and pick up several "tricks" to make SAS do your bidding.
My stab
data w;
k="000112010302";
do _n_=1 to length(k);
highest=max(highest,char(k,_n_));
end;
run;
Well, I'm not sure that can be beat here. Here was mine, but it's a few lines longer.
%macro m(); data q; s = '000112010302'; %do i = 1 %to 12; c&i. = substr(s,&i.,1); %end; max = max(of c1-c12); drop c1-c12 i; run; %mend; %m;
Well, @ProcWes, it looks like you've been lured by the siren song of SAS macro. Macro is tempting for its reusability, but if you turned on MPRINT you'd see that this code actually generates a lot more statements.
If we allow that we know the length of the string going in, then we can use the max of construct as you've done.
data _null_;
c = '000112010302';
array chars{12} 3;
do i = 1 to length(c);
chars(i) = input(substr(c,i,1),1.);
end;
answer = max(of chars[*]);
put answer=;
run;
But so far @novinosrin has got these both beat.
I suspect that SAS/IML can probably do even better -- load the chars into a matrix and then get the max.
@ChrisHemedinger- yeah, I just wanted the c&i loop to work horizontally. If I do a regular do, I can't get that - it executes vertically. (i can't reference the "i" value for the variable name.)
I don't use arrays enough to set it up, but i'm not surprised there is an option there.
Chris,
Methinks FreelanceReinhard clearly wins this race since his offer doesn't even require looping. With a loop, infinite variants are possible. For example:
data _null_ ; c = '000112010302' ; do j = 0 to 9 until (length (c) = 1) ; c = compress (c, put (j, 1.)) ; end ; put c= ; run;
Or just:
data _null_ ; c = '000112010302' ; do j = 1 to length (c) ; d = d <> input (char (c, j), 1.) ; end ; put d=; run;
And so forth ...
Paul D.
Thanks a lot for chiming in, Paul. This is a great honor.
I'm hesitant to point out that the UNTIL condition length (c) = 1 would not be effective in the case of a tie.
The honor more than deserved.
And you shouldn't be hesitant about the UNTIL. Thou art speaketh the truth.
proc iml;
str='000112010302';
maximum=max((substr(str,1:length(str),1)));
print maximum;
quit;
Here's my crack at it in IML. Credit the assist to https://blogs.sas.com/content/iml/2014/05/05/iml-character-vectors.html
Chance beat me to the punch. Not sure what the rules are ... do we just need to compute it? Print it?
proc iml;
k="000112010302";
m=max(substr(k,1:nleng(k),1));
/* OR if need print */
proc iml;
k="000112010302";
print (max(substr(k,1:nleng(k),1)));
How about this?
%sysfunc(findc(123456789,000112010302,b));
Or, in a data step:
data;
c='000112010302';
h=findc('123456789',c,-9);
run;
(Edit: replaced 'b' by -9 in the data step to save one more character)
Edit 2: At the cost of readability, one could replace '123456789' with 1e10/81-1 (two characters less).
Editor's note: this was the earliest, best solution that came in -- so it's the Accepted Solution. But it's worth reading through ALL of the responses to learn about different approaches and pick up several "tricks" to make SAS do your bidding.
It doesn't get any terser and more ingenious than that. Kudos.
That might be the shortest, but it doesn't handle an empty string properly. It treats a missing string as if it was all 0 digits.
Here is a version using FINDC() that returns -1 for missing strings.
data have;
input k $12.;
x=10-findc('9876543210 ',k);
y=findc('123456789',k,-9);
put k $12. +1 x= y=;
cards;
000112010302
0
1
2
3
4
5
6
7
8
9
0123456789
.
;
You are a code poet, sir! Nicely done.
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.