Obsidian | Level 7

## Calculating length of variables part 2

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

Thanks (again)

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

## Re: Calculating length of variables part 2

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

3 REPLIES 3
Super User

## Re: Calculating length of variables part 2

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...
Super User

## Re: Calculating length of variables part 2

```if index(variable,'00') > 0 then <however you are indicating unclassified>;
Else <the approach chosen earlier to find the first/last combination>;```
PROC Star

## Re: Calculating length of variables part 2

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

Discussion stats
• 3 replies
• 488 views
• 2 likes
• 4 in conversation