BookmarkSubscribeRSS Feed
GN0001
Barite | Level 11

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

Blue Blue
14 REPLIES 14
HarrySnart
SAS Employee

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 

GN0001
Barite | Level 11

Hello Harry,

This didn't work. it gave me 17.

Thanks for your input anyway.

Respectfully,

Blue

Blue Blue
HarrySnart
SAS Employee

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.

 

HarrySnart_0-1685657713493.png

 

Is it giving you 17 when you apply it to your real dataset?


Thanks

Harry

GN0001
Barite | Level 11
Yes correct, it gave me number 17 on my own data.
I used it in in substr,
Thanks for it,
Respectfully,
Blue blue
Blue Blue
PaigeMiller
Diamond | Level 26

The ANYALPHA function would also work here

--
Paige Miller
GN0001
Barite | Level 11

Hello, 

I tried to use anyalpha, but I couldn't.

Can you please kindly guide me?

Regards,

blue

Blue Blue
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
GN0001
Barite | Level 11
I acknowledged it, but input data was member Id. If I share it, I can be jailed. My input data is personal health information, I can’t share it. I have mentioned it several times.
I always Google before posting my question to this forum if I can’t find the answer, or if I find the answer, again I want to make sure that what I am trying to do is 100 percent correct.
Please kindly bear with me.
Regards
Blue
Blue Blue
Kurt_Bremser
Super User

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.

GN0001
Barite | Level 11
I used Anyalpha, that gave me number 17.
I need to use this in a substr function, to get all the string till 17 or till whatever number Anyalpha gives.
Regards,
blue
Blue Blue
HarrySnart
SAS Employee

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;

 

HarrySnart_0-1685658233292.png

 

Tom
Super User Tom
Super User

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.

 

Patrick
Opal | Level 21

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;

Patrick_0-1685698845874.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 2231 views
  • 8 likes
  • 6 in conversation