please provide the below mentioned
In a dataset there are two variables ,with one of the variable has parentheses around them. Combine these into a single numeric variable called " and remove the parentheses and add a leading zero
You may try:
Phone_number = input(compress(area_code||number, '()' ) , best.);
format phone_number z12.; /* adapt format length as need */
The question is misleading, as it is really not possible. There is no such thing as a numeric variable that contains a leading zero. If you know the length of a variable, you can print it with leading zeros but that's only the printing format. The variable itself does not contain any leading zeros.
TRANSLATE function handles this pretty easily but creates a character variable:
newvar = translate(oldvar, ' 0', ')(' );
As you haven't confirmed what the data looks like, will still have to guess:
data want;
set have; var1="(1234)"; var2=5555; result=input(cats(tranwrd(var1,"()"),put(var2,best.)),best.);
format result z9.; run;
This only displays the data using Zx. format to show the extra 0, as said above numeric values do not have the ability to hold preceeding zeroes.
So you will need to do more of the work here. There are too many pieces that could be defined one way or the other.
Regarding the inputs: Is NUMBER numeric or character? Most phone numbers look more like 555-2134 with a dash in the middle that means it is character. What is actually in your data? If there is a dash there originally, do you want it removed?
Regarding the output: There still is no such thing as a numeric variable that contains a leading zero. So what do you want? A list of digits in a file, where the first digit is a zero?
Give a specific example showing what one line of input looks like, and what the corresponding output should be.
You may try:
Phone_number = input(compress(area_code||number, '()' ) , best.);
format phone_number z12.; /* adapt format length as need */
A good idea is to post test data - in the form of a datastep - and what the output should look like. This is so that we - who don't know what you have/are doing - can try to understand the problem and the required outcome.
Your first variable will be character - numeric variables cannot have () in them. Is the second variable numeric or character - you see this is where the test data in the form of a datastep comes in.
Numeric values do not have preceeding zeros, this can only be displayed using a format - Zx. shows numeric values , to length x, with zeroes padded at the front. You can also put this result into a character if you want to retain the format outside the SAS system.
So something like:
data want;
length result $50; set have; var1="(1234)"; var2=5555; result=cats(put(tranwrd(var1,"()"),z6.),put(var2,best.)); run;
Will put var1 without () into 6 characters padding the front with zeroes to make the length, then concatenating the var2 to this to form a final character result. You could convert this reult to numeric and apply a Zx. format where x is the total length of var1+var2 if you like.
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.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.