title 'FunctionsIII Example 3 - Listing of Data Set HOAGIE';
data hoagie;
input string $10.;
length sub1 $5.;
sub1= substr(string,2,5);
sub2= substr(string,-1,4);
sub3= substr(string,3,0);
sub4= substr(string,7,5);
sub5= substr(string,0,2);
file print;
title " Demostrating the substrn function ";
put "Original String = " @25 string /
"substrn(string,2,5) = " @25 sub1 /
"substrn(string,-1,4) = "@25 sub2 /
"substrn(string,3,0) = " @25 sub3 /
"substrn(string,7,5) = " @25 sub4 /
"substrn(string,0,2) = " @25 sub5;
datalines;
ABCDEFGHIJ
;
run;
proc print;
run;
DATA HOAGIE;
STRING = 'ABCDEFGHIJ';
LENGTH RESULT $5.;
RESULT = SUBSTRN(STRING,2,5);
SUB1 = SUBSTRN(STRING,-1,4);
SUB2 = SUBSTRN(STRING,3,0);
SUB3 = SUBSTRN(STRING,7,5);
SUB4 = SUBSTRN(STRING,0,2);
FILE PRINT;
TITLE "Demonstrating the SUBSTRN Function";
PUT "Original String =" @25 STRING /
"SUBSTRN(STRING,2,5) =" @25 RESULT /
"SUBSTRN(STRING,-1,4) =" @25 SUB1 /
"SUBSTRN(STRING,3,0) =" @25 SUB2 /
"SUBSTRN(STRING,7,5) =" @25 SUB3 /
"SUBSTRN(STRING,0,2) =" @25 SUB4;
RUN;
proc print;
run;
The main difference between the two sets of code that I noticed (didn't look too closely though) are that the first set uses both the substr and substrn functions, while the second only used the substrn function.
The differences between the two functions are described at: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002255112.htm
Art, CEO, AnalystFinder.com
Thanks
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.