BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nimu
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

6 REPLIES 6
Astounding
PROC Star

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

nimu
Calcite | Level 5
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.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

Astounding
PROC Star

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.

Shmuel
Garnet | Level 18

You may try:

 

Phone_number = input(compress(area_code||number,  '()' ) , best.);

format phone_number z12.;   /* adapt format length as need */

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

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!

How to Concatenate Values

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.

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
  • 6 replies
  • 3353 views
  • 0 likes
  • 4 in conversation