Search code examples
sassubstr

How to scan the string and convert dynamically in SAS


Supposed I have two strings to convert from SAS program name to table number.

My goal is to convert the first "f-2-2-7-5-vcb" to "2.2.7.5". And this should be done dynamically. Like for "f-2-2-12-1-2-hbd87q", it needed to be "2.2.12.1.2" .

How to accomplish this?

data input; 
input str $ 1-20; 
datalines;
f-2-3-1-5-vcb
f-2-4-1-6-rtg
f-2-3-11-1-3-hb17
;
run;

data want;
 set input;
 Sub=compress(substr(str,3,length(str)),,'kd') ;
run;

Solution

  • Bit of a longer way, but this works fine for me.

    • Use FIND() to find the first '-'
    • Use REVERSE() and FIND() to find the last '-'
    • Use SUBSTR() and metrics + math from above to remove the first and last components
    • Use TRANSLATE() to convert the - to periods.

       z=find(str, '-');
       end=find(strip(reverse(str)), '-');
       string = translate(substr(str, z+1, length(str) - z - end), ".", "-");