Hi, Cheri:
The PICTURE format is expecting that your phone number is numeric, not character. If you are getting an error message similar to this:
ERROR: You are trying to use the numeric format PHONE with the character variable xxxxxxxxx.
Then that is one problem. If, however, your phone number IS numeric and you are not getting your format used, then than is a different problem.
Let's start with some data:
Obs name phonenum charphone
1 alan 2222222 2222222
2 bob 9999999 9999999
3 cathy 3103333333 3103333333
4 dave 1234567 1234567
5 eliza 104567890 0104567890
PHONENUM is a numeric variable, you can tell because it is RIGHT-justified and CHARPHONE is a character variable, you can tell because it is LEFT-justified.
Once you DEFINE your format, using the format statement shown above, then you can USE the format. It may not entirely be clear where you define or use the format when you go to insert code in EG. But here's a few notes that may help:
1) when you DEFINE a format, it is a self-contained unit of code that starts with the keywords "PROC FORMAT" and ends with a "RUN". That unit of code must appear in your task code BEFORE you need to use the PICTURE format.
2) when you USE the format, it is generally used WITHIN some other procedure. So the FORMAT statement or a FORMAT= option will appear within some other PROC step (like PROC SQL or PROC PRINT or PROC FREQ). See the example below. Let's say that the data file above is called WORK.TESTPHONE.
proc format;
picture phone
2000000-9999999 = '999-9999'
9999999<-9999999999 = '999 999-9999'
other = 'miscoded';
run;
** now, use the format with a format statement;
proc print data=work.testphone;
title 'Testing Phone Number Picture Format';
var name phonenum charphone;
format phonenum phone. ;
run;
As you can see...the PROC FORMAT step is self-contained and sits OUTSIDE the PROC PRINT step.
This is code which you would normally generate with the LIST DATA task in EG.
However, the FORMAT statement, which USES the PHONE. format sits INSIDE the PROC PRINT step.
You cannot use the PHONE. format with the CHARPHONE variable, because PHONE. is a numeric format, and SAS will give you an error message if you try to use a numeric format with a character variable. So, what do you do if you have a character variable and you need to apply a PICTURE format to it??? In that case, you have to convert the format using an INPUT statement (this is the code for a DATA step program):
convert_ph = input(charphone,10.);
The new variable CONVERT_PH is now a numeric variable and could be formatted with the PHONE. format. The following example with PROC SQL shows creating a new numeric variable from CHARPHONE and assigning the format within PROC SQL.
** proc format step would go here;
** THEN, use the format with PROC SQL in a query;
proc sql;
create table work.newphone as
select name,
phonenum format=phone.,
input(charphone,10.) as newphnum format=phone.
from work.testphone
order by name;
quit;
proc print data=work.newphone;
title 'Data made with SQL -- do not need format statement here';
title2 'because it was assigned in the SQL step above';
var name phonenum newphnum;
run;
In this example, I don't show the PROC FORMAT step again, but in order to run this second bit of code, you would need to have created the format in the same session or have the PROC FORMAT code, as a self-contained unit sitting OUTSIDE the PROC SQL step. The method of creating a new variable inside the PROC SQL is only a bit different from the assignment above. But basically, you are still using the INPUT function to convert CHARPHONE into a new numeric variable called NEWPHNUM. The PROC PRINT results are shown below for both datasets.
cynthia
Hi, Cheri:
The PICTURE format is expecting that your phone number is numeric, not character. If you are getting an error message similar to this:
ERROR: You are trying to use the numeric format PHONE with the character variable xxxxxxxxx.
Then that is one problem. If, however, your phone number IS numeric and you are not getting your format used, then than is a different problem.
Let's start with some data:
Obs name phonenum charphone
1 alan 2222222 2222222
2 bob 9999999 9999999
3 cathy 3103333333 3103333333
4 dave 1234567 1234567
5 eliza 104567890 0104567890
PHONENUM is a numeric variable, you can tell because it is RIGHT-justified and CHARPHONE is a character variable, you can tell because it is LEFT-justified.
Once you DEFINE your format, using the format statement shown above, then you can USE the format. It may not entirely be clear where you define or use the format when you go to insert code in EG. But here's a few notes that may help:
1) when you DEFINE a format, it is a self-contained unit of code that starts with the keywords "PROC FORMAT" and ends with a "RUN". That unit of code must appear in your task code BEFORE you need to use the PICTURE format.
2) when you USE the format, it is generally used WITHIN some other procedure. So the FORMAT statement or a FORMAT= option will appear within some other PROC step (like PROC SQL or PROC PRINT or PROC FREQ). See the example below. Let's say that the data file above is called WORK.TESTPHONE.
proc format;
picture phone
2000000-9999999 = '999-9999'
9999999<-9999999999 = '999 999-9999'
other = 'miscoded';
run;
** now, use the format with a format statement;
proc print data=work.testphone;
title 'Testing Phone Number Picture Format';
var name phonenum charphone;
format phonenum phone. ;
run;
As you can see...the PROC FORMAT step is self-contained and sits OUTSIDE the PROC PRINT step.
This is code which you would normally generate with the LIST DATA task in EG.
However, the FORMAT statement, which USES the PHONE. format sits INSIDE the PROC PRINT step.
You cannot use the PHONE. format with the CHARPHONE variable, because PHONE. is a numeric format, and SAS will give you an error message if you try to use a numeric format with a character variable. So, what do you do if you have a character variable and you need to apply a PICTURE format to it??? In that case, you have to convert the format using an INPUT statement (this is the code for a DATA step program):
convert_ph = input(charphone,10.);
The new variable CONVERT_PH is now a numeric variable and could be formatted with the PHONE. format. The following example with PROC SQL shows creating a new numeric variable from CHARPHONE and assigning the format within PROC SQL.
** proc format step would go here;
** THEN, use the format with PROC SQL in a query;
proc sql;
create table work.newphone as
select name,
phonenum format=phone.,
input(charphone,10.) as newphnum format=phone.
from work.testphone
order by name;
quit;
proc print data=work.newphone;
title 'Data made with SQL -- do not need format statement here';
title2 'because it was assigned in the SQL step above';
var name phonenum newphnum;
run;
In this example, I don't show the PROC FORMAT step again, but in order to run this second bit of code, you would need to have created the format in the same session or have the PROC FORMAT code, as a self-contained unit sitting OUTSIDE the PROC SQL step. The method of creating a new variable inside the PROC SQL is only a bit different from the assignment above. But basically, you are still using the INPUT function to convert CHARPHONE into a new numeric variable called NEWPHNUM. The PROC PRINT results are shown below for both datasets.
cynthia
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.