Hello team,
I have a string as this: 1234568F888.
I need to find the position of the character in this string.
Any help is greatly appreciated.
Blue
hi @GN0001
I'd use prxmatch to get the index.
example below
data _null_;
have = "1234568F888";
want = prxmatch('([A-Za-z])',have);
put want;
run;
thanks
harry
Hello Harry,
This didn't work. it gave me 17.
Thanks for your input anyway.
Respectfully,
Blue
Hi Blue
That's odd it gave you 17. I've just re-ran it in my environment, it gives me the correct position at 8.
Is it giving you 17 when you apply it to your real dataset?
Thanks
Harry
The ANYALPHA function would also work here
Hello,
I tried to use anyalpha, but I couldn't.
Can you please kindly guide me?
Regards,
blue
@GN0001 wrote:
Hello,
I tried to use anyalpha, but I couldn't.
Can you please kindly guide me?
Regards,
blue
We have talked about this. Do not say it doesn't work without giving more information — specifically we need you to provide the code you tried, the input data (if it is not in the code itself) and the output. And we shouldn't have to ask for this in every thread.
For the data you presented, the suggested methods work, period.
Applying those methods to your real data is now your task. Different data, different results.
If you need further help, supply usable example data (DATA step with DATALINES) where you make up fake data which resembles the original data structure, and show the expected result from this fake data.
Hi @GN0001 ,
if you're getting 17 with both PRXPARSE and ANYALPHA then it sounds like you just need to do the substring from this?
You can feed the output of either approach into the SUBSTR function.
data _null_;
have = "1234568F888";
want = prxmatch('([A-Za-z])',have);
put "character at: " want;
/* substring until char */
want2 = substr(have,1,want-1); *everything until F;
want3 = substr(have,want+1); *everything after F;
put want2;
put want3;
run;
So are you using a double byte character set? That might return 17 instead of 9 since each character would take 2 bytes in the string the 9 character would start in the 17th byte.
Or perhaps your string has leading spaces that you are not seeing either because you use PUT with the $ format instead of the $CHAR or $QUOTE format. Or you looked that the ODS output instead of the LISTING (PRINT) output.
If you're really dealing with a multibyte character set then you need to tell us clearly. For a multibyte character set one can only use string functions on I18N Level 2.
Which functions are suitable is fully documented here.
From what you write I believe you want the substring of digits up-to the first character in the string. If so then why not use the scan() function ...or kscanx() for multibyte.
data test;
infile datalines truncover;
input str $20.;
first_char_pos=kfindc(str,,'kd');
digits_until_first_char=kscanx(str,1,'','kd');
datalines;
1234568F888
;
proc print data=test;
run;
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.