DATA Step, Macro, Functions and more

remove parentheses and add a leading zero

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

remove parentheses and add a leading zero

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


Accepted Solutions
Solution
‎12-17-2016 07:37 PM
Trusted Advisor
Posts: 1,584

Re: remove 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 */

View solution in original post


All Replies
Super User
Posts: 5,516

Re: remove parentheses and add a leading zero

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', ')(' );

New Contributor
Posts: 2

Re: remove parentheses and add a leading zero

Posted in reply to Astounding
Hi,

The actual questions is

1. You have a dataset called "Telephone_Number" which includes two
variables "Area_Code" and "Number". The "Area_Code" has parentheses around
them. Combine these into a single numeric variable called "Home_Number" and
remove the parentheses and add a leading zero.
Super User
Super User
Posts: 7,988

Re: remove parentheses and add a leading zero

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. 

Super User
Posts: 5,516

Re: remove parentheses and add a leading zero

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.

Solution
‎12-17-2016 07:37 PM
Trusted Advisor
Posts: 1,584

Re: remove 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 */

Super User
Super User
Posts: 7,988

Re: remove parentheses and add a leading zero

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.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 380 views
  • 0 likes
  • 4 in conversation