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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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