Hi,
I would like to select all binary 0/1 variables from my dataset and apply yesno. format to them.
All suggestions are welcome.
Many thanks!
@Kurt_Bremser wrote:
- Identify numeric variables (DICTIONARY.COLUMNS)
- Run PROC FREQ on all of them, use OUT= to create a dataset
- Analyze the dataset to find all those that have only the two values, and store their names in a macro variable
- Use the macro variable in PROC DATASETS
Proc freq with the NLELEVLS may be useful as well.
Consider:
data example; input var1 $ var2 var3 var4 var5; datalines; 1 1 0 1 1 b 0 0 0 1 1 1 1 1 2 b 0 1 1 0 ; proc freq data=example nlevels ; ods output nlevels=levelset; tables _numeric_; run;
The data set Levelset will have the variable name along with the number of levels the variables have in the Nlevels variable.
So any variable with Nlevels > 2 could be excluded from further processing though confirmation that the values. Coupled with a Proc Means data set built with STACKODS and Min / Max the process for numeric values can be pretty straightforward.
data example; input var1 $ var2 var3 var4 var5; datalines; 1 1 0 1 1 b 0 0 0 1 1 1 1 1 2 b 0 1 1 0 ; proc freq data=example nlevels ; ods output nlevels=levelset (rename=(tablevar = variable)) ; tables _numeric_; run; Proc means data=example stackodsoutput min max; ods output summary=summaryset; var _numeric_; run; data want; merge levelset summaryset ; by variable; if nlevels=2 and min=0 and max=1; run;
The Want data set has one row for each variable that has exactly 2 levels of values with the minimum=0 and maximum=1.
So you can select the variables names how you want such as a list created here that could be used in a format statement like
Format &varlist. yesno. ; as needed/
proc sql noprint; select variable into :varlist separated by ' ' from want ; quit; %put Variable list is: &varlist. ;
Either write a custom format using Proc Format or simple if-then-else logic.
Depends on the situation. Post more information if you want a more detailed answer.
format variablename1 variablename2 variablename3 yesno.;
where obviously, you have to type in the actual variable names in your data set, or use a valid SAS list
format x1-x27 yesno.;
@pink_poodle wrote:
Hi,
I would like to select all binary 0/1 variables from my dataset and apply yesno. format to them.
All suggestions are welcome.
Many thanks!
Is this code supposed to examine your data set, find variables that only have 1 and 0 values? What if the values are missing for some records? What if the value is character '1' and '0'? What if one or more of the variables that your are thinking are 0/1 only have 0 for a value? Or only 1 for a value?
@Kurt_Bremser wrote:
- Identify numeric variables (DICTIONARY.COLUMNS)
- Run PROC FREQ on all of them, use OUT= to create a dataset
- Analyze the dataset to find all those that have only the two values, and store their names in a macro variable
- Use the macro variable in PROC DATASETS
Proc freq with the NLELEVLS may be useful as well.
Consider:
data example; input var1 $ var2 var3 var4 var5; datalines; 1 1 0 1 1 b 0 0 0 1 1 1 1 1 2 b 0 1 1 0 ; proc freq data=example nlevels ; ods output nlevels=levelset; tables _numeric_; run;
The data set Levelset will have the variable name along with the number of levels the variables have in the Nlevels variable.
So any variable with Nlevels > 2 could be excluded from further processing though confirmation that the values. Coupled with a Proc Means data set built with STACKODS and Min / Max the process for numeric values can be pretty straightforward.
data example; input var1 $ var2 var3 var4 var5; datalines; 1 1 0 1 1 b 0 0 0 1 1 1 1 1 2 b 0 1 1 0 ; proc freq data=example nlevels ; ods output nlevels=levelset (rename=(tablevar = variable)) ; tables _numeric_; run; Proc means data=example stackodsoutput min max; ods output summary=summaryset; var _numeric_; run; data want; merge levelset summaryset ; by variable; if nlevels=2 and min=0 and max=1; run;
The Want data set has one row for each variable that has exactly 2 levels of values with the minimum=0 and maximum=1.
So you can select the variables names how you want such as a list created here that could be used in a format statement like
Format &varlist. yesno. ; as needed/
proc sql noprint; select variable into :varlist separated by ' ' from want ; quit; %put Variable list is: &varlist. ;
I recommend you study the documentation of the DATASETS Procedure, particularly the MODIFY Statement, where you use the FORMAT Statement as usual.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.