BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
archibald
Obsidian | Level 7
Hi , 
I need to add trailing zeros and period field, any help would be appreciated.
 
data have;
Input alpha : $5.;
cards;
00100
1019
099
185
1010
0803
12398;
run;
 
data want;
set have;
test1=substr(alpha,1,3)||'.'|| substr(alpha,4,2); /* add a period to fields*/
 
run;
 In addition  to adding a period ,  I would  want to add trailing zeros so to have :
test1
001.00
101.90
099.00
185.00
101.00
080.30
123.98;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data have;
Input alpha : $5.;
cards;
00100
1019
099
185
1010
0803
12398
;
run;

data want;
set have;
substr(alpha,length(alpha)+1) = '00000';
length test1 $6;
test1 = substr(alpha,1,3) !! '.' !! substr(alpha,4);
run;

View solution in original post

6 REPLIES 6
sowmya
Fluorite | Level 6

You could read the data as below:

data have;
Input alpha;
cards;
00100
1019
099
185
1010
0803
12398;
run;
 
data formatted;
format new z6.2;
set have;
new=alpha/100;
run;
 
This should work, however in the example u gave the data is not consistant. For example, if the first row has trainling zeros, how come rest of them dont have.
archibald
Obsidian | Level 7

@sowmya not all the data will need a trailing zeros, as you can see on what the result should look like. 

Thanks!

Kurt_Bremser
Super User
data have;
Input alpha : $5.;
cards;
00100
1019
099
185
1010
0803
12398
;
run;

data want;
set have;
substr(alpha,length(alpha)+1) = '00000';
length test1 $6;
test1 = substr(alpha,1,3) !! '.' !! substr(alpha,4);
run;
archibald
Obsidian | Level 7

This is perfect!Thank you very much!

andreas_lds
Jade | Level 19

Just replace the blanks in alpha with zeros an your code will produce the required resulsts.

 

data want;
set have;

alpha = translate(alpha, '0', ' ');
test1=substr(alpha,1,3)||'.'|| substr(alpha,4,2); /* add a period to fields*/
 
run;
archibald
Obsidian | Level 7

Another great solution. Thank youvery much!

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
  • 6 replies
  • 2694 views
  • 2 likes
  • 4 in conversation