Hello
I apply Format on numeric column.
Why the field is changing to alphanumeric?
For example:
Data tbl;
input ID score;
cards;
1 7
2 8
3 6
4 11
;
Run;
proc format;
value FFMT;
0='1'
1-7='2'
8-10='3'
11='4';
run;
data tbl2;
set tbl;
format score FFMT.;
Run;
What will be the difference If I define the format without quotes ''
proc format;
value Ffmt
0=1
2-7=2
8-10=3
11=4;
run;
Whether you use quotes in the VALUE or INVALUE statement does not impact what type of FORMAT you are creating. The quotes are optional only to maintain backwards compatibility with older versions of SAS. But it is good coding style to use the quotes around the text strings and not use them around the numbers.
To create a character format use the VALUE and a $ prefix on the format name.
value $FFMT ...
To create a character informat use the INVALUE statement and a $ prefix on the informat name.
invalue $FFMT ...
A format converts values into text, so the label (right of the equal sign) should be in quotes for both numeric and character formats.
An informat converts text into values, so the range specification (left of the equal sign) should be in quotes.
A numeric format works on numbers, so the range specification (left of the equal sign) should NOT be in quotes.
A numeric informat generates numbers, so the label (right of the equal sing) should NOT be in quotes.
Formats only change the appearance of the values in the field. Formats do not change the actual value, and formats do not change a variable from numeric to character.
Run a proc contents and you will see that the variable is still numeric. Formats do not change the type of the variable to which they are attached, only the display.
What is the expected variable type of x_new1 and x_new2?
I expected that x_new2 will be char and x_new1 will be numeric.
Data a;
input ID x;
cards;
1 7
2 11
3 8
4 4
5 10
;
run;
proc format;
value ffmt
0=1
2-10=2
11=3
;
run;
proc format;
value ff2mt;
0='1'
2-10='2'
11='3'
;
run;
data b;
set a;
x_new1=put(x,ffmt.);
x_new2=put(x,ff2mt.);
Run;
2
PUT() takes a numeric variable and creates a new character variable.
PUT() cannot result in a numeric variable.
This is clearly and explicitly stated in the documentation for PUT, which says:
Use the PUT function to convert a numeric value to a character value.
Formats convert values to text. Informats convert text to values.
Numeric formats convert numbers to text. Character formats convert text to text.
Numeric informats convert text to numbers. Character informats convert text to text.
To convert numbers to numbers you need to use a format and then an informat.
x_new1=input(put(x,ffmt.),32.);
So you did not use an existing variable wich somehow magically changed its type, you CREATED A NEW VARIABLE by assigning the result of a PUT() function. That will always result in the new variable being of type character, because the PUT() function returns a character value.
Whether you use quotes in the VALUE or INVALUE statement does not impact what type of FORMAT you are creating. The quotes are optional only to maintain backwards compatibility with older versions of SAS. But it is good coding style to use the quotes around the text strings and not use them around the numbers.
To create a character format use the VALUE and a $ prefix on the format name.
value $FFMT ...
To create a character informat use the INVALUE statement and a $ prefix on the informat name.
invalue $FFMT ...
A format converts values into text, so the label (right of the equal sign) should be in quotes for both numeric and character formats.
An informat converts text into values, so the range specification (left of the equal sign) should be in quotes.
A numeric format works on numbers, so the range specification (left of the equal sign) should NOT be in quotes.
A numeric informat generates numbers, so the label (right of the equal sing) should NOT be in quotes.
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.