Hi,
I have a numeric variable taking the values 0,1 and "." . When using the function compress or cats for this variable, the "." is tranformed into a 0. Is there any way to avoid this aside from transforming the numeric variable into a character variable taking the value "0" , "1" and ".".
Thank you 😉
The COMPRESS() function operates on character strings. Why would you ever use it on a numeric value?
CATS and COMPRESS are character functions, which do not work well with numeric variables.
A missing value in SAS is, per default, represented as a dot. You can change how SAS displays a missing value with the MISSING= system option. If you get a 0 instead of the dot when using the implicit conversion from numeric to character, then most likely your MISSING option is set to a zero.
For detailed help, post your code and log, and some example data. Please post your example data as a data step with datalines, so it is easy for us to recreate your dataset in our environment.
Use this button:
for posting logs (and other textual data that must be kept as-is), and the "little running man" right next to it for SAS code.
How a missing value is rendered in output is controlled with the system option MISSING.
You can examine the current setting of the option by looking in the LOG
Example:
proc options option=MISSING; run;
Log
133 proc options option=missing; 134 run; SAS (r) Proprietary Software Release 9.4 TS1M6 MISSING=. Specifies the character to print for missing numeric values. NOTE: PROCEDURE OPTIONS used (Total process time):
The option allows you to specify the single character to use.
Example:
data have; input x @@; datalines; 0 1 . ; options missing = . nosource; proc options option=missing; run; data _null_; set have; result = cats (x); put 'NOTE:' x= result=; run; options missing = 0; * Show missing values as 0 instead of default .; data _null_; set have; result = cats (x); put 'NOTE:' x= result=; run; options missing = !; * Show missing values as exclamation point (!); data _null_; set have; result = cats (x); put 'NOTE:' x= result=; run; options missing = . source;
Log (the X= value is the missing value rendering, and the RESULT= value is the rendered missing stored in a variable)
SAS (r) Proprietary Software Release 9.4 TS1M6 MISSING=. Specifies the character to print for missing numeric values. NOTE: PROCEDURE OPTIONS used (Total process time): real time 0.00 seconds cpu time 0.00 seconds NOTE:x=0 result=0 NOTE:x=1 result=1 NOTE:x=. result=. <--------------- NOTE: There were 3 observations read from the data set WORK.HAVE. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds NOTE:x=0 result=0 NOTE:x=1 result=1 NOTE:x=0 result=0 <--------------- NOTE: There were 3 observations read from the data set WORK.HAVE. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds NOTE:x=0 result=0 NOTE:x=1 result=1 NOTE:x=! result=! <--------------- NOTE: There were 3 observations read from the data set WORK.HAVE. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Large economy sized hint:
Provide some example data and how you want the output to appear.
Since you mention CATS function then that seems to imply that you have multiple variables.
Here's a start of data set with 3 variables and all combinations of ., 0, and 1.
data have; input v1 v2 v3; datalines; . . . . . 0 . . 1 . 0 . . 0 0 . 0 1 . 1 . . 1 0 . 1 1 0 . . 0 . 0 0 . 1 0 0 . 0 0 0 0 0 1 0 1 . 0 1 0 0 1 1 1 . . 1 . 0 1 . 1 1 0 . 1 0 0 1 0 1 1 1 . 1 1 0 1 1 1
;
So now you just have to show what the result should be.
The CATX() function should so that. Make sure the missing option is set to '.' and not something else, like '0' or ' '.
%let missing=%sysfunc(getoption(missing));
options missing='.';
data have;
do v1=.,0,1; do v2=.,0,1; do v3=.,0,1;
v4 = catx('_',of v1-v3);
output;
end;end;end;
run;
options missing="&missing";
proc print;
run;
Obs v1 v2 v3 v4 1 . . . ._._. 2 . . 0 ._._0 3 . . 1 ._._1 4 . 0 . ._0_. 5 . 0 0 ._0_0 6 . 0 1 ._0_1 7 . 1 . ._1_. 8 . 1 0 ._1_0 9 . 1 1 ._1_1 10 0 . . 0_._. 11 0 . 0 0_._0 12 0 . 1 0_._1 13 0 0 . 0_0_. 14 0 0 0 0_0_0 15 0 0 1 0_0_1 16 0 1 . 0_1_. 17 0 1 0 0_1_0 18 0 1 1 0_1_1 19 1 . . 1_._. 20 1 . 0 1_._0 21 1 . 1 1_._1 22 1 0 . 1_0_. 23 1 0 0 1_0_0 24 1 0 1 1_0_1 25 1 1 . 1_1_. 26 1 1 0 1_1_0 27 1 1 1 1_1_1
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.