DATA Step, Macro, Functions and more

adding trailing zeros & period to fields

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

adding trailing zeros & period to fields

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;

Accepted Solutions
Solution
‎05-18-2016 09:07 PM
Super User
Posts: 7,809

Re: adding trailing zeros & period to fields

Posted in reply to archibald
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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Occasional Contributor
Posts: 8

Re: adding trailing zeros & period to fields

Posted in reply to archibald

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.
Contributor
Posts: 62

Re: adding trailing zeros & period to fields

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

Thanks!

Solution
‎05-18-2016 09:07 PM
Super User
Posts: 7,809

Re: adding trailing zeros & period to fields

Posted in reply to archibald
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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 62

Re: adding trailing zeros & period to fields

Posted in reply to KurtBremser

This is perfect!Thank you very much!

Super Contributor
Posts: 345

Betreff: adding trailing zeros & period to fields

Posted in reply to archibald

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;
Contributor
Posts: 62

Betreff: adding trailing zeros & period to fields

Posted in reply to andreas_lds

Another great solution. Thank youvery much!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 460 views
  • 2 likes
  • 4 in conversation