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)
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.
if index(variable,'00') > 0 then <however you are indicating unclassified>; Else <the approach chosen earlier to find the first/last combination>;
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.
Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.
Explore Now →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.