BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mauri0623
Quartz | Level 8

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

.

.

.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

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?

Kurt_Bremser
Super User

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
novinosrin
Tourmaline | Level 20
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;
mkeintz
PROC Star

@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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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
  • 4 replies
  • 865 views
  • 3 likes
  • 5 in conversation