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

Hello,

This is my fist post, so please excuse any lack of etiquette. My question is how to remove the trailing zeros on a string. The following is my fictional dataset and below that is how I wish it would look after a datastep. Ignore the decimal point part of the code, since I need that for a subsequent step not related to dropping the trailing zeros. Also, the final dataset should be formatted as characters.

data icd9;

     input id diag$;

     length newdiag $6;

     newdiag = substr(diag,1,3)||"."||

     substr(diag,4,2);

     datalines;

     1     80500

     2     80555

     3     80550

     4     e8500

     5     e8550

     6     e8555

     7     v8500

     8     v8550

     9     v8555

     10     08500

     11     08550

     12     08555

     13     0414

     14     041 

     15     04144

     ;

run;

Desired final product:

1805
2805.55
3805.5
4e85
5e85.5
6e85.55
7v85
8v85.5
9v85.55
10085
11085.5
12085.55
13041.4
14041
15041.44
1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19
newdiag = substrn(diag,1,max(3,f));

View solution in original post

13 REPLIES 13
data_null__
Jade | Level 19

This look about right.

data icd9;
   input id diag$;
   length newdiag $6;
   f = findc(diag,
'0','TBK');
   newdiag = substrn(diag,1,max(3,f));
   newdiag = catx('.',substrn(newdiag,1,3),substrn(newdiag,4));
   datalines;
     1     80500
     2     80555
     3     80550
     4     e8500
     5     e8550
     6     e8555
     7     v8500
     8     v8550
     9     v8555
     10     08500
     11     08550
     12     08555
     13     0414
     14     041   
     15     04144
     ;
   run;
proc print;
  
run;

Message was edited by: data _null_

H
Pyrite | Level 9 H
Pyrite | Level 9

Thank you for the prompt post. Yes, this does seem to be what I was trying to achieve. Much thanks, in that this would have taken me a long time to figure out myself.

H
Pyrite | Level 9 H
Pyrite | Level 9

My error, I thought that I included all of the scenarios but forgot about the following:

Hello,

This is my fist post, so please excuse any lack of etiquette. My question is how to remove the trailing zeros on a string. The following is my fictional dataset and below that is how I wish it would look after a datastep. Ignore the decimal point part of the code, since I need that for a subsequent step not related to dropping the trailing zeros. Also, the final dataset should be formatted as characters.

data icd9;

     input id diag$;

     length newdiag $6;

     newdiag = substr(diag,1,3)||"."||

     substr(diag,4,2);

     datalines;

     1     80500

     2     80555

     3     80550

     4     e8500

     5     e8550

     6     e8555

     7     v8500

     8     v8550

     9     v8555

     10     08500

     11     08550

     12     08555

     13     0414

     14     041  

     15     04144

     16     80000

     17     85000

     18     04000

     ;

run;

Desired final product:

1805
2805.55
3805.5
4e85
5e85.5
6e85.55
7v85
8v85.5
9v85.55
10085
11085.5
12085.55
13041.4
14041
15041.44
16800
17850
18040
data_null__
Jade | Level 19
newdiag = substrn(diag,1,max(3,f));
Haikuo
Onyx | Level 15

Another approach using Prxchange:

data icd9;

   input id diag$;

   length _new $6;

   _new=catx('.',substr(diag,1,3),prxchange('s/([^0]?)(0+$)/$1/io',-1,compress(substr(diag,4))));

   datalines;

     1     80500

     2     80000

     3     80550

     4     e8500

     5     e8550

     6     e8555

     7     v8500

     8     v8550

     9     v40

     10     08500

     11     08550

     12     08555

     13     0414

     14     041   

     15     04144

     ;

   run;

Haikuo

Patrick
Opal | Level 21

I prefer data null's solution - but here another variant using a bit a simpler RegEx:

data icd9;
  input id diag :$5.;
  length newdiag $ 6;
  newdiag=prxchange('s/0{1,2} *$//oi',1,diag);
  if length(newdiag)>3 then
    newdiag = substrn(newdiag,1,3)||"."||substrn(newdiag,4);
  datalines;
1 80500
2 80555
3 80550
4 e8500
5 e8550
6 e8555
7 v8500
8 v8550
9 v8555
10 08500
11 08550
12 08555
13 0414
14 041
15 04144
;
run;

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

What does the 'TBK' do,data_null_? Smiley Happy

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

thanks

H
Pyrite | Level 9 H
Pyrite | Level 9


Thank you everyone. All three options worked great and are very helpful.

AnnaBrown
Community Manager

I'm glad you found some solutions, H, and your etiquette is right on. Thanks for marking "Correct" and "Helpful" answers. Those are most useful for others who may be searching for answers to a similar problem.

Anna


Join us for SAS Community Trivia
SAS Bowl XXIX, The SAS Hackathon
Wednesday, March 8, 2023, at 10 AM ET | #SASBowl

H
Pyrite | Level 9 H
Pyrite | Level 9


Well since I received such expeditious and straightforward help, I am going to add hopefully one last twist. Since this seems to remain in the same content area, I won't opt to make this as a new thread. My question is as follows:

How would I best repeat this for for multiple variables/columns. I have diag1-diag60, and I would like all 60 variables transformed as discussed above? I could obviously do each one independently or in one big swoop. Though, I have not quite figured out how to do it with the multiple statements and variable names. Any help would be greatly appreciated.

Haikuo
Onyx | Level 15

Using an array should help, something like the following:

data icd9;

   input id (diag1-diag4) (:$);

   array diag diag1-diag4;

   array _new $8 _new1-_new4;

   do over _new;

   _new=catx('.',substr(diag,1,3),prxchange('s/([^0]?)(0+$)/$1/io',-1,compress(substr(diag,4))));

   end;

   datalines;

     1     80500     80500       80500     80500   

     2     80000    80000    80000    80000

     3     80550    80550    80550    80550

     4     e8500    e8500    e8500    e8500

     5     e8550    e8550    e8550    e8550

     6     e8555    e8555    e8555    e8555

     7     v8500    v8500    v8500    v8500

     8     v8550    v8550    v8550    v8550

     9     v40      v40    v40      v40

     10     08500     08500     08500     08500

     11     08550     08550     08550     08550

     12     08555     08555     08555     08555

     13     0414     0414     0414     0414

     14     041       041         041       041  

     15     04144     04144     04144     04144

     ;

   run;

Haikuo

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 13 replies
  • 9487 views
  • 10 likes
  • 6 in conversation