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

Dear All, 

 

I have a large dataset with diagnosis codes that are up to four digits. I need to make them more uniform by adding zeros preceding the first digit (letter) to make them 4 digits but in a character format. 

 

I've tried this but I end up with all zeros.

 

data diagnosis;

input diagnosis;

datalines;

094

16

E4

;

run;

 

data diagnosis;

set diagnosis;

diagnose=translate(right(diagnosis),'0',' ');

;

run;

 

My desired output would be this:

0094
0016
00E4

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

First of all, your first data step won't really work, as you try to create a numeric variable, so add a dollar sign:

data diagnosis;
input diagnosis $;
datalines;
094
16
E4
;
run;

Then try this:

data diagnosis;
set diagnosis;
length newdiag $4;
newdiag = '0000';
substr(newdiag,5-length(diagnosis)) = diagnosis;
drop diagnosis;
rename newdiag=diagnosis;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

First of all, your first data step won't really work, as you try to create a numeric variable, so add a dollar sign:

data diagnosis;
input diagnosis $;
datalines;
094
16
E4
;
run;

Then try this:

data diagnosis;
set diagnosis;
length newdiag $4;
newdiag = '0000';
substr(newdiag,5-length(diagnosis)) = diagnosis;
drop diagnosis;
rename newdiag=diagnosis;
run;
GKati
Pyrite | Level 9

Perfect. This works well. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26
data want;
  var="E4";
  want=cats(repeat('0',3-lengthn(var)),var);
run;
Ksharp
Super User
data diagnosis;
input diagnosis $;
datalines;
094
16
E4
;
run;
data diagnosis;
set diagnosis;
length newdiag $4;
newdiag = diagnosis;
newdiag= translate(right(newdiag),'0',' ');
run;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 14337 views
  • 0 likes
  • 4 in conversation