BookmarkSubscribeRSS Feed
RamKumar
Fluorite | Level 6

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

11 REPLIES 11
BrunoMueller
SAS Super FREQ

Have a look at the Zw. format

RamKumar
Fluorite | Level 6

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

user24feb
Barite | Level 11

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;

RamKumar
Fluorite | Level 6

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

user24feb
Barite | Level 11

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;

RamKumar
Fluorite | Level 6

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;

user24feb
Barite | Level 11

!! 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 (!!).

Patrick
Opal | Level 21

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

RamKumar
Fluorite | Level 6

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 11 replies
  • 4439 views
  • 0 likes
  • 5 in conversation