BookmarkSubscribeRSS Feed
DSAS_er
Calcite | Level 5

Hi

 

I'm looking for a SAS code to remove the trailing 0's from the end of a character, where it applies.  Please see the following as an example:

 

Have

1111-2020-00555

1111-2021-546

1111-2020-00789

1111-2020-01234

1111-2020-343

 

Want:

1111-2020-555

1111-2021-546

1111-2020-789

1111-2020-1234

1111-2020-343

 

 

4 REPLIES 4
Reeza
Super User

Did you by chance create this variable using CATX()? If so, you could fix it in that step.

 

data want;
set have;

want = catx('-', input(scan(have, 1, "-"), 8.), input(scan(have, 2, "-"), 8.), input(scan(have, 3, "-"), 8.));

run;

@DSAS_er wrote:

Hi

 

I'm looking for a SAS code to remove the trailing 0's from the end of a character, where it applies.  Please see the following as an example:

 

Have

1111-2020-00555

1111-2021-546

1111-2020-00789

1111-2020-01234

1111-2020-343

 

Want:

1111-2020-555

1111-2021-546

1111-2020-789

1111-2020-1234

1111-2020-343

 

 


 

ballardw
Super User

You need to provide a consistent RULE.

I can write code that matches your example but probably will not work for any other values.

 

And why does your subject line say "leading zeroes" and the description in body say "trailing zeroes"???

 

I might guess this would work:

data have;
   input x :$15.;
datalines;
1111-2020-00555
1111-2021-546
1111-2020-00789
1111-2020-01234
1111-2020-343
;

data want;
   set have;
   x=cats(substr(x,1,10),input(substr(x,11),best.));
run;

Please note the data step to provide example data. Without an actual example with properties of the variable we have to make guesses. And please post code or data in a code box opened with the </> icon on the forum. The main message window will remove stuff and insert html codes. Sometimes what you paste is not what you expect and code won't work against it.

 

Consider my "example" below:

Have    Want
1           3
2           3
3           5
4           4
5           4
6           3

Can you tell that the result for 7 should be 5, or that 21 should be 9?

mklangley
Lapis Lazuli | Level 10

Here's one way to do it, using INPUT() to remove leading zeros:

data have;
    input variable $20.;
    datalines;
    1111-2020-00555
    1111-2021-546
    1111-2020-00789
    1111-2020-01234
    1111-2020-343
    ;
run;

data want (drop=last_dash);
    set have;
    last_dash = length(variable) - length(scan(variable,-1,'-'));
    new_variable = substr(variable,1,last_dash) || left(put(input(substr(variable,last_dash),best.),$20.));
run;

 

Or another method, using VERIFY():

data want (drop=last_dash);
    set have;
    last_dash = length(variable) - length(scan(variable,-1,'-'));
    new_variable2 = substr(variable,1,last_dash) || substr(substr(variable,last_dash+1),verify(substr(variable,last_dash+1),'0'));
run;

 

r_behata
Barite | Level 11

Is this what you are looking for ?

 

data have;
input str : $16.;
cards;
1111-2020-00555
1111-2021-546
1111-2020-00789
1111-2020-01234
1111-2020-343
;
run;

data want;
	set have;

	str=prxchange('s/-0+/-/io',-1,str);

	put (_all_) (=);
run;

Log :

 

28         data want;
29         	set have;
30         
31         	str=prxchange('s/-0+/-/io',1,str);
32         
33         	put (_all_) (=);
34         run;


str=1111-2020-555
str=1111-2021-546
str=1111-2020-789
str=1111-2020-1234
str=1111-2020-343

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
  • 686 views
  • 3 likes
  • 5 in conversation