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
Have a look at the Zw. format
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
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;
If my input is character, then how to add leading zeros for the variable with character datatype?
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;
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;
!! 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 (!!).
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;
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;
If my input is character, then how to add leading zeros for the variable with character datatype?
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.