I have the following strings. I want to get rid of the C at the end. This is because another file that has this variable does not contain the C and when it is merged creates problem. I want to get rid of the C at the end.
09120192019 0800000 C
047 X4540001 C
013 X4512000 C
02020192019 0919000 C
.
.
.
A little play with length() and substr():
data have;
input string $30.;
datalines;
09120192019 0800000 C
047 X4540001 C
013 X4512000 C
02020192019 0919000 C
;
data want;
set have;
if substr(string,length(string)-1,2) = ' C'
then string = substr(string,1,length(string)-2);
run;
proc print data=want noobs;
run;
Result:
string 09120192019 0800000 047 X4540001 013 X4512000 02020192019 0919000
So if the last character of the string is 'C' then you want to get rid of it? But not if it is an 'A' for example?
A little play with length() and substr():
data have;
input string $30.;
datalines;
09120192019 0800000 C
047 X4540001 C
013 X4512000 C
02020192019 0919000 C
;
data want;
set have;
if substr(string,length(string)-1,2) = ' C'
then string = substr(string,1,length(string)-2);
run;
proc print data=want noobs;
run;
Result:
string 09120192019 0800000 047 X4540001 013 X4512000 02020192019 0919000
data have;
input string $30.;
datalines;
09120192019 0800000 C
047 X4540001 C
013 X4512000 C
02020192019 0919000 C
;
data want;
set have;
want=substr(string,1,findc(string,'c','ib')-1);
run;
@mauri0623 wrote:
I have the following strings. I want to get rid of the C at the end. This is because another file that has this variable does not contain the C and when it is merged creates problem. I want to get rid of the C at the end.
Maybe you don't have to get rid of the trailing "C". Since the other variable would match everything but the trailing C, you could match based on the "EQT" (equal-truncated) operator, as in
data table1;
input id @13 matchval $11. ;
datalines;
09120192019 0800000 C
047 X4540001 C
013 X4512000 C
02020192019 0919000 C
run;
data table2;
input name $4. matchval $11.;
put (_all_) (=);
datalines;
DAVE 0800000
MARK X4512000
ALEX ABCDEF
run;
proc sql;
create table want as select t1.*,t2.name
from table1 as t1 inner join table2 as t2
on t1.matchval EQT t2.matchval;
quit;
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.