Help using Base SAS procedures

How to print a numeric SAS variable with leading zeros?

Reply
Regular Contributor
Posts: 168

How to print a numeric SAS variable with leading zeros?

I've a SAS variable which is numeric with the same length for all records and I need to modify with one leading zeros and it still be numeric for all records like below.

As Is:

Mobilenumber

9167642524

9167642525

9167642526

To Be:

Mobilenumber

09167642524

09167642525

09167642526

Please suggest  me to achieve this task

SAS Super FREQ
Posts: 709

Re: How to print a numeric SAS variable with leading zeros?

Have a look at the Zw. format

Regular Contributor
Posts: 168

Re: How to print a numeric SAS variable with leading zeros?

Posted in reply to Bruno_SAS

I already seen the Zw. format but I'm not sure how it can be incorporated in SAS code as it is been often used with Input and Put function

Super Contributor
Posts: 340

Re: How to print a numeric SAS variable with leading zeros?

Use Format for Proc Print and Put to create a String:

Data A;

  Input Phone;

  Format Phone Z11.;

  Phone_Text=Put(Phone,Z11.);

  Datalines;

9167642524

9167642525

9167642526

;

Run;

Regular Contributor
Posts: 168

Re: How to print a numeric SAS variable with leading zeros?

Posted in reply to user24feb

If my input is character, then how to add leading zeros for the variable with character datatype?

Super Contributor
Posts: 340

Re: How to print a numeric SAS variable with leading zeros?

I would prefer the Phone_Text-version which transforms the character to numeric over the Phone_Text2-version:

Data A;

  Input Phone $;

  Phone_Text=Put(Input(Phone,Best11.),Z11.);

  Phone_Text2=Repeat('0',10-Length(Phone))!!Phone; /* use a Macro-Variable instead of 10 */

  Datalines;

9167642524

9167642525

9167642526

642526

;

Run;

Regular Contributor
Posts: 168

Re: How to print a numeric SAS variable with leading zeros?

Posted in reply to user24feb

thanks, but in the interest of time could you please tell me the significance of !! in the line Phone_Text2=Repeat('0',10-Length(Phone))!!Phone;

Super Contributor
Posts: 340

Re: How to print a numeric SAS variable with leading zeros?

!! is a concatenating operator. I use it instead of vertical bars (||). The reason is, that my keyboard does not have a solid vertical bar, so I use either two broken vertical bars (¦¦) or  two exclamation marks (!!).

Respected Advisor
Posts: 4,173

Re: How to print a numeric SAS variable with leading zeros?

Hope one of below options will do for you:

data have;

  infile datalines truncover;

  input @1 Phone_Char $11. @1 Phone_Num 11.;

  Phone_Char2=cats('0',Phone_Char);

  Phone_Char3=cats(repeat('0',10-lengthn(Phone_Char)),Phone_Char);

  Phone_Char4=put(input(Phone_Char,11.),z11.);

  Phone_Char5=prxchange('s/^(\d{10}) +$/0\1/oi',1,Phone_Char);

  Phone_Num2=Phone_Num;

  format Phone_Num2 z11.;

  datalines;

9167642524

9167642525

9167642526

19167642526

;

run;

Super User
Super User
Posts: 7,988

Re: How to print a numeric SAS variable with leading zeros?

You could apply it to the number variable, or you can create a new character variable and put the number into the Z format.  Or you could apply the format in proc report etc.

data want;

     set have;

     format mobilenumber z11.;

run;

Regular Contributor
Posts: 168

Re: How to print a numeric SAS variable with leading zeros?

If my input is character, then how to add leading zeros for the variable with character datatype?

Super User
Super User
Posts: 7,988

Re: How to print a numeric SAS variable with leading zeros?

Hi,

Patrick has been nice enough to provide many examples of this.  The !! means concatenate.  What the code is doing is checking the existing length of Pone.  Then if less than 10 create a string of 0's to pad out to 10 length and concatenate these to Phone.

Also, it does not matter if you input is character and you want character out, you can still do put(input()) as User24feb has stated: Phone_Text=Put(Input(Phone,Best11.),Z11.);

This converts the string to number, uses Z. format to padd it, then puts it back to character and assigns the character result back into your original character format.  So you end up with the original variable with the data padded.

Ask a Question
Discussion stats
  • 11 replies
  • 1087 views
  • 0 likes
  • 5 in conversation