BookmarkSubscribeRSS Feed
rk7
Obsidian | Level 7 rk7
Obsidian | Level 7

 

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;

 

title 'FunctionsIII Example 3 - Listing of Data Set HOAGIE';
93 data hoagie;
94 input string $10.;
95 length sub1 $5.;
96 sub1= substr(string,2,5);
97 sub2= substr(string,-1,4);
98 sub3= substr(string,3,0);
99 sub4= substr(string,7,5);
100 sub5= substr(string,0,2);
101 file print;
102 title " Demostrating the substrn function ";
103 put "Original String = " @25 string /
104 "substrn(string,2,5) = " @25 sub1 /
105 "substrn(string,-1,4) = "@25 sub2/
106 "substrn(string,3,0) = " @25 sub3/
107 "substrn(string,7,5) = " @25 sub4/
108 "substrn(string,0,2) = " @25 sub5;
109 datalines;
 
NOTE: Invalid second argument to function SUBSTR at line 97 column 8.
NOTE: Invalid third argument to function SUBSTR at line 98 column 8.
NOTE: Invalid third argument to function SUBSTR at line 99 column 8.
NOTE: Invalid second argument to function SUBSTR at line 100 column 8.
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
110 ABCDEFGHIJ
string=ABCDEFGHIJ sub1=BCDEF sub2= sub3=CDEFGHIJ sub4=GHIJ sub5= _ERROR_=1 _N_=1
NOTE: 6 lines were written to file PRINT.
NOTE: The data set WORK.HOAGIE has 1 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.05 seconds
 
111 ;
 
112 run;
113
114 proc print;
115 run;
 
NOTE: There were 1 observations read from the data set WORK.HOAGIE.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.04 seconds
cpu time 0.04 seconds
 
 
116
117 DATA HOAGIE;
118 STRING = 'ABCDEFGHIJ';
119 LENGTH RESULT $5.;
120 RESULT = SUBSTRN(STRING,2,5);
121 SUB1 = SUBSTRN(STRING,-1,4);
122 SUB2 = SUBSTRN(STRING,3,0);
123 SUB3 = SUBSTRN(STRING,7,5);
124 SUB4 = SUBSTRN(STRING,0,2);
125 FILE PRINT;
126 TITLE "Demonstrating the SUBSTRN Function";
127 PUT "Original String =" @25 STRING /
128 "SUBSTRN(STRING,2,5) =" @25 RESULT /
129 "SUBSTRN(STRING,-1,4) =" @25 SUB1 /
130 "SUBSTRN(STRING,3,0) =" @25 SUB2 /
131 "SUBSTRN(STRING,7,5) =" @25 SUB3 /
132 "SUBSTRN(STRING,0,2) =" @25 SUB4;
133 RUN;
 
NOTE: 6 lines were written to file PRINT.
NOTE: The data set WORK.HOAGIE has 1 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.03 seconds
 
 
134 proc print ;
135 run;
 
NOTE: There were 1 observations read from the data set WORK.HOAGIE.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds
 
 
136
137
138 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
151
 
2 REPLIES 2
art297
Opal | Level 21

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

 

rk7
Obsidian | Level 7 rk7
Obsidian | Level 7

Thanks

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 732 views
  • 1 like
  • 2 in conversation