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 have another problem with calculating differences in lengths between 2 variables.

For example, I have a field called 'Emp' with 6 characters as follows;

 

010111

111001

101010

100010

 

I am trying to calculate the length between the first appearance of a '1' and the last appearance of a '1' for each row. However, if we have a string of at least two zeros in between the first and last 1 for each row, I want to classify this observation as 'Unclassified'.

 

The anwers should be 4 (for the 1st row), 'Unclassifed' (for the 2nd row), 4 (for the 3rd row) and 'Unclassified' (for the 4th row).

 

Can somebody please help out with a code to automate this process?

 

Thanks (again)

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

You already got an answer to part 1, along these lines:

 


first=findc(str,'1');
last=findc(str,'1','b');
want=last-first;

For part 2, add one last statement:

 

if index(substr(str, first, want), '00') then want = .U;

Note that WANT is defined as numeric.  So it can't store "unclassified" as its value.  But it can store a special missing value (.U) to indicate that it fits into that category.

View solution in original post

3 REPLIES 3
Reeza
Super User
1. Use FINDC() to return the position of the first 1.
2. Use REVERSE() + FINDC() to find the position of the last 1. Use 6 - this value to get the actual position.
3. Use SUBSTR() and #1/#2 to get the middle code and then use FIND() to search for 00 in the string.

So you just need some SAS basic string functions.

Functions are documented here:
https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=allprodsle&docsetTarget=s...
ballardw
Super User
if index(variable,'00') > 0 then <however you are indicating unclassified>;
Else <the approach chosen earlier to find the first/last combination>;
Astounding
PROC Star

You already got an answer to part 1, along these lines:

 


first=findc(str,'1');
last=findc(str,'1','b');
want=last-first;

For part 2, add one last statement:

 

if index(substr(str, first, want), '00') then want = .U;

Note that WANT is defined as numeric.  So it can't store "unclassified" as its value.  But it can store a special missing value (.U) to indicate that it fits into that category.

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!

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
  • 488 views
  • 2 likes
  • 4 in conversation