@yabwon wrote:
Hi @ScottBass
there is one more "funny" question, that was always bugging me.
Character format and character informat - why two of them, is there any difference I'm missing that we need them?
All the bets
Bart
proc format;
invalue $ abc
"A"="a"
"B"='b'
C = c
other = "_"
;
value $ abc
"A"="a"
"B"='b'
C = c
other = "_"
;
run;
data Test;
do letter = "A","B","C","D";
f= put(letter,$abc.);
i=input(letter,$abc.);
output;
end;
run;
Hi @yabwon,
Character format: expects character input, always returns character output
Character informat: always expects character input, returns character output
So sure, if you have character input and are desiring character output, then either a character format or informat will work (i.e. give you the desired results).
You might get different results between the two if you munged the input/output data types, i.e. character format assigning to a numeric variable, or character informat with numeric input. Sometimes SAS will do the implicit type conversion, and sometimes it generates an error, depending on the combination of function, in/format, input and output variable type. I myself never like implicit type conversion, and I HATE errors :). So, I always try to explicitly convert the type myself via INPUT/PUT. Note that unnecessary type conversions (i.e. using a format when you should use an informat to return a numeric value) will affect performance.
However, character informats (INVALUE statement) have additional options, such as JUST and UPCASE:
* character format: expects character input, always returns character output ;
* character informat: always expects character input, returns character output ;
proc format;
* character informat, expects character input, always returns character output ;
invalue $ abc
"A"="a"
"B"='b'
C = c
other = "_"
;
* character format, always expects character input, returns character output ;
value $ abc
"A"="a"
"B"='b'
C = c
other = "_"
;
* testing: do quotes even matter? (nope, and is documented);
invalue $ noq
A=a
B=b
C=c
other=_
;
value $ noq
A=a
B=b
C=c
other=_
;
* informats also have the upcase and just option ;
invalue $just (just)
A=X
B=Y
C=Z
other=_
;
invalue $upc (upcase)
A=X
B=Y
C=Z
other=_
;
invalue $justupc (just upcase)
A=X
B=Y
C=Z
other=_
;
%macro comment;
value $upc (upcase) /* but not formats, which makes sense */
A=a
B=b
C=c
other=_
;
%mend comment; * comment out using uncalled macro ;
* a numeric informat ;
invalue num
1=1
2=2
3=3
other=.Z
;
run;
data Test1;
do letter = "A","B","C","D";
f= put(letter,$abc.);
i=input(letter,$abc.);
output;
end;
run;
data Test2;
do letter = "A","B","C","D";
f= put(letter,$noq.);
i=input(letter,$noq.);
output;
end;
run;
data Test3A; * justify ;
length letter $5;
do letter = "A"," B"," C"," D";
i=input(letter,$just5.);
output;
end;
run;
data Test3B; * upcase ;
length letter $5;
do letter = "a","b","c","d";
i=input(letter,$upc5.);
output;
end;
run;
data Test3C; * justify+upcase ;
length letter $5;
do letter = "a"," b"," c"," d";
i=input(letter,$justupc5.);
output;
end;
run;
data Test4;
do num=1 to 4;
i=input(num,num.); * implicit type conversion for the input function ;
i2=input(put(num,best.),num.); * explicit type conversion for the input function ;
output;
end;
run;
Getting the Test3's to give the correct results was tricky; I finally remembered to add a length suffix to the informats, since the default length is based on the input values (with a little help from Google and http://support.sas.com/resources/papers/proceedings10/022-2010.pdf)
As you noted in your code, the quotes are optional. From the doc:
https://documentation.sas.com/?docsetId=proc&docsetTarget=p1pmw90bl3jzgdn1w4202kclxtho.htm&docsetVersion=9.4&locale=en
If you omit the single or double quotation marks around character-string, then the INVALUE statement assumes that the quotation marks are there.
The VALUE statement says:
You must enclose a formatted value in single or double quotation marks. The following example shows a formatted value that is enclosed in double quotation marks
But the above shows that is not always the case. I thought they would be required if the label contains spaces, but that does not appear to be the case:
proc format;
* testing: do quotes even matter? ;
invalue $ noq
A=a a a
B=b
C=c
other=_
;
value $ noq
A=a
B=b b b
C=c
other=_
;
run;
data Test2;
do letter = "A","B","C","D";
f= put(letter,$noq.);
i=input(letter,$noq.);
output;
end;
run;
The above appears to work, but doesn't give the correct results if you do:
proc format;
value $ noq
A=a
B=b b b
C C C=c
other=_
;
quit;
Best practice is to follow the SAS documentation regarding quotation marks.
HTH...
... View more