BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pink_poodle
Barite | Level 11

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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. ;

 

 

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

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.

PaigeMiller
Diamond | Level 26
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.;
--
Paige Miller
ballardw
Super User

@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?

pink_poodle
Barite | Level 11
That is right. My intention was to find all binary variables in the dataset and then apply the yesno format to them. Usually they are numeric 0/1. I kind of found a not-so-elegant way of Find-Replacing them in Word reports, but am still curious about achieving this task before the output to Word.
Kurt_Bremser
Super User
  • 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
ballardw
Super User

@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. ;

 

 

pink_poodle
Barite | Level 11
Could you please elaborate on the fourth point about PROC DATASETS?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 866 views
  • 6 likes
  • 5 in conversation