DATA Step, Macro, Functions and more

Add Trailing 0

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Add Trailing 0

Hi Team,

 

I have a Zip Code  1235

                               0234

 

Need result as        12350000

                                02340000


Accepted Solutions
Solution
a month ago
Super User
Super User
Posts: 9,227

Re: Add Trailing 0

Posted in reply to msharma1788

Post test data in the form of a datastep.  I am now having to guess if that zip code is a character or numeric variable!  if its numeric then you convert it character:

data want;
  set have;
  new_zip=put(zip_code,z8.);
run;

If its already character:

data want;
  set have;
  new_zip=cats(new_zip,"0000");
run;

 

View solution in original post


All Replies
Solution
a month ago
Super User
Super User
Posts: 9,227

Re: Add Trailing 0

Posted in reply to msharma1788

Post test data in the form of a datastep.  I am now having to guess if that zip code is a character or numeric variable!  if its numeric then you convert it character:

data want;
  set have;
  new_zip=put(zip_code,z8.);
run;

If its already character:

data want;
  set have;
  new_zip=cats(new_zip,"0000");
run;

 

Regular Contributor
Posts: 153

Re: Add Trailing 0

Hi,

 

this will do the trick:

data have;
zip='1235';output;
zip='0234';output;
run;
data want;
   length newzip $10;
   set have;
   newzip=strip(zip)||repeat('0',7-lengthn(strip(zip)));
run;


________________________

- Cheers -

Super User
Posts: 10,626

Re: Add Trailing 0

Posted in reply to msharma1788
data have;
zip='1235';output;
zip='0234';output;
run;
data want;
   length zip $ 8;
   set have;
   newzip=translate(left(zip),'0',' ');
run;
Super User
Posts: 13,084

Re: Add Trailing 0

Posted in reply to msharma1788

@msharma1788 wrote:

Hi Team,

 

I have a Zip Code  1235

                               0234

 

Need result as        12350000

                                02340000


I have to say that is the oddest Zip code requirement unless this is a non-United States postal code. US Zipcodes are either 5 character or 5digits a dash and 4 digits: 12345-7890 example.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 102 views
  • 0 likes
  • 5 in conversation