This is where using a negative value for the position parameter of the SCAN function is just the right tool to get the last component (because a negative number starts from the last position and works towards the first). Then, to get the first component, apply TRANWRD to the original text to "translate" the last component to a blank.
data _null_;
original_text="West of arrell street 1/2/SAN PEDRO CA 90731";
last_component=scan(original_text,-1,'/');
first_component=tranwrd(original_text,cats('/',last_component),'');
put (_all_) (=);
run;
The only flaw in this would be if the string composing the last_component appears more than once, which would remove the first instance. You can avoid this by locating the text position of last character that precedes the "/" that demarks the last component (actually one character to the left), then just copy a substring of the original text through that position:
data _null_;
original_text="West of arrell street 1/2/SAN PEDRO CA 90731";
last_component=scan(original_text,-1,'/');
first_component=substr(original_text,1, length(original_text)-length(last_component)-1);
put (_all_) (=);
run;
This is where using a negative value for the position parameter of the SCAN function is just the right tool to get the last component (because a negative number starts from the last position and works towards the first). Then, to get the first component, apply TRANWRD to the original text to "translate" the last component to a blank.
data _null_;
original_text="West of arrell street 1/2/SAN PEDRO CA 90731";
last_component=scan(original_text,-1,'/');
first_component=tranwrd(original_text,cats('/',last_component),'');
put (_all_) (=);
run;
The only flaw in this would be if the string composing the last_component appears more than once, which would remove the first instance. You can avoid this by locating the text position of last character that precedes the "/" that demarks the last component (actually one character to the left), then just copy a substring of the original text through that position:
data _null_;
original_text="West of arrell street 1/2/SAN PEDRO CA 90731";
last_component=scan(original_text,-1,'/');
first_component=substr(original_text,1, length(original_text)-length(last_component)-1);
put (_all_) (=);
run;
Hello @Deb4 and welcome to the SAS Support Communities!
Just determine the position of that last delimiter (FINDC function) and then extract the two parts of the string using the SUBSTR function.
Example:
data have;
addr='West of arrell street 1/2/SAN PEDRO CA 90731';
run;
data want(drop=p);
set have;
p=findc(addr,'/','b');
addr1=substr(addr, 1, p-1);
addr2=left(substr(addr, p+1));
run;
I used the LEFT function to ensure that ADDR2 does not contain leading blanks. Feel free to define shorter lengths (than the default, which is the defined length of ADDR) for ADDR1 and ADDR2 with a LENGTH statement.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.