LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (April 2008, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 9 Apr 2008 14:22:33 -0700
Reply-To:     Bill McKirgan <Bill.McKirgan@GMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Bill McKirgan <Bill.McKirgan@GMAIL.COM>
Organization: http://groups.google.com
Subject:      Re: How to add zeros ahead of numeric data
Comments: To: sas-l@uga.edu
Content-Type: text/plain; charset=ISO-8859-1

On Apr 9, 3:52 pm, jn_...@YAHOO.COM (jn mao) wrote: > Hi SAS-Ls, > > I have some character type's number data. I need to add '00' or '0' before the numbers and still keep them as charcter type data. > > Examples: > raw data (chacter type): > 8.8 > 34.5 > wanted data (character type): > 00880 > 03450 > > The raw data are character type data, so I did convert them to numberic data, then time 100, eg. 8.8---> 880, and then I convert them back to character data, then I use CAT function to add '00' ahead the character data. However, there always with many spaces between the '00' and the numbers, like 00 880 > > How do I delete these spaces? and how can I have the right format data? > > Here is my code for adding zeros for new character data: > > raw_num=input(raw_cha,best.) > raw_num=raw_num*100; > > new_cha=put(raw_num, best14.) > if 0<raw_num<10 then new_cha=cat('00',new_cha); > if 10<raw_num<100 then new_cha=cat('0',new_cha); > > Thanks much, > Jane > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection aroundhttp://mail.yahoo.com

The z. format is helpful here. I had to fiddle around abit to see what needed to be done to get the decimal removed and insert the desired trailing zeros as well, but I think this might work for what you described.

data raw; input a $; cards; 8.8 34.5 ;

data wanted; set raw;

a_num=input(a,4.1); format a_num z6.1; *put a @10 a_num;

b = compress(put(a_num,z6.2),'.'); put b;

run;

--log--

00880 03450 NOTE: There were 2 observations read from the data set WORK.RAW. NOTE: The data set WORK.WANTED has 2 observations and 3 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds


Back to: Top of message | Previous page | Main SAS-L page