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

Hello,

 

I'm hoping somebody can help me out with something that seems fairly simple! 

 

I have a character variable called 'Emp' which is 12 characters long and contains the numbers 1 and 0 only. For example;

 

111111111111

110111111111

111111111110

011111111111

011011110110

 

I am trying to work out the difference between the position of the first '1' and the last '1' for each row. The answers should be 11 (for the 1st row), 11 (for the 2nd row), 10 (for the 3rd row), 10 (for the 4th row) and 9 (for the 5th row).

 

Can somebody help me put this in to a code without having to go through each line manually?

 

Thank you 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Fun stuff

 

Hi @zishaq 

 

data have;
input str $12.;
cards;
111111111111
110111111111
111111111110
011111111111
011011110110
;

data want;
set have;
first=findc(str,'1');
last=findc(str,'1','b');
want=last-first;
run;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

Fun stuff

 

Hi @zishaq 

 

data have;
input str $12.;
cards;
111111111111
110111111111
111111111110
011111111111
011011110110
;

data want;
set have;
first=findc(str,'1');
last=findc(str,'1','b');
want=last-first;
run;
zishaq
Obsidian | Level 7
Thanks a lot!
PaigeMiller
Diamond | Level 26
data want;
    set have;
    length_of_string=length(x);
    first_one=find(x,'1');
    last_one=length_of_string-find(reverse(x),'1')+1;
    distance=last_one-first_one;
run;
--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 459 views
  • 2 likes
  • 3 in conversation