BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I want to find first location of character in string.

Lets say I have the following data.

Vector='0,0,5,11,9,10'

I want to find the first location (after comma) where the number is different than 0.

In this case the answer is  3  because the third argument in vector has value different than 0.

 

How can I calculate it in SAS  please?

 

 

 

thanks

Ronein

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data want (drop=i);
  Vector='0,0,5,11,9,10';
  do i=1 to countw(vector,',');
    if input(scan(vector,i,','),best.) > 0 and word = . then word=i;
  end;
run;

 

 

 

Patrick
Opal | Level 21

@RW9's code with a tweak to stop looping once a word has been found.

data want (drop=_:);
  Vector='0,0,5,11,9,10';
  _n=countw(vector,',');
  do _i=1 to _n;
    if input(scan(vector,_i,','),best.) > 0 then
      do;
        word=_i;
        leave;
      end;
  end;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Quite true, but then you would only need:

data want;
  vector='0,0,5,11,9,10';
  do word=1 to countw(vector,',');
    if input(scan(vector,word,','),best.) > 0 then leave;
  end;
run;

As word gets incremented up to the point where the loop is exited.

Patrick
Opal | Level 21

@RW9

And what's the value of Word if the condition never gets True?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

True, I had assumed there would always be one non-zero.  

s_lassen
Meteorite | Level 14

This is one of the occasions where PRX is really good:

data want;
  Vector='0,0,5,11,9,10';
  pos=prxmatch('/\b[1-9]/',Vector);
run;

A short explanation of the search string: \b means a word boundary (could be your comma, or the beginning of the string), [1-9] means one of the digits 1 to 9. If there is no match, the function returns 0.

Patrick
Opal | Level 21

@s_lassen wrote:

This is one of the occasions where PRX is really good:

data want;
  Vector='0,0,5,11,9,10';
  pos=prxmatch('/\b[1-9]/',Vector);
run;

A short explanation of the search string: \b means a word boundary (could be your comma, or the beginning of the string), [1-9] means one of the digits 1 to 9. If there is no match, the function returns 0.


@s_lassen

...except that it returns 5 instead of 3. The OP asks for the number of the Word (3rd word) and not the start position in the string.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 1121 views
  • 4 likes
  • 4 in conversation