BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
novinosrin
Tourmaline | Level 20

To nail using SAS functions is a bit of a challenge until the pattern is absolute unlike a relatively cleaner PRX(Regular expression). So

data have;
input x_all :$20. expected ; 
cards;
11110000011 4
111001111 9
111001111000 9 
;
run;
data want;
 set have;
 want=ifn(0<count(substr(x_all,1,findc(x_all,'1','b')),'0')=2,
 lengthn(substr(x_all,1,findc(x_all,'1','b'))),
 max(0,findc(x_all,'0')-1));
run;
Reeza
Super User

Seems to me this logic simplifies to finding the first 0. 

If it's the first character you get a 0. If it's not the first 0, then you subtract 1 to find the position of the last 1 which is the sum of the values up to that point. 

If you find no 0's, that means you have all ones, which is the length of the string. 

 

data want;
set have;

x_total = findc(x_all, '0');

if x_total = 0 then x_total = length(x_all);
else if x_total > 0 then x_total - 1;

run;
Tom
Super User Tom
Super User

Do you want to sum the digits or just count them? 

You can use the VERIFY() function to find the first location that is not a '1'.  Make sure to append something in case all of string is filled with ones.  NOTE: Your first example as 7 ones, not six.

data test;
  input str :$20. wrong right ;
  want = verify(str||'0','1')-1 ;
cards;
1111111000000   6  6
0000001111111   7  0
1111000111111  10  4
;
Obs         str         wrong    right    want

 1     1111111000000       6       6        7
 2     0000001111111       7       0        0
 3     1111000111111      10       4        4

 

silversta
Calcite | Level 5
Thank you all for your responses, this was most helpful!
yabwon
Onyx | Level 15

Hi,

 

Just for fun:

data have;
input x_all :$20.; 
cards;
1111111000000
0000001111111
1111000111111
;
run;

data want;
 set have;
 want = lengthn(prxchange('s/^(1*)(0*)(.*)/$1/', -1, x_all));
run;    

Bart

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

...and one more:

data want1;
 set have;
 want = lengthn(scan(x_all,1,"0","M"));
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



novinosrin
Tourmaline | Level 20

Thank you @yabwon  for your time and contribution. Much appreciate it.

This one too perhaps?

  want=max(0,findc(x_all,'0')-1);

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
  • 22 replies
  • 2615 views
  • 18 likes
  • 7 in conversation