DATA Step, Macro, Functions and more

Cross Freq every var in a dataset

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

Cross Freq every var in a dataset

Hi SAS friends,

I have a much larger version of the following dataset and my task is to perform cross-frequency tables on every binary variable (e.g. Has_Income HasGender etc..). i'm looking to capture every combination but I consider HasGender*HasIncome the same as HasIncome*HasGender. Can you help me solve this one? Smiley Happy

data Have;

  input ID $2. HaveAge HaveGender HaveIncome;

cards;

1 0 0 0

2 1 1 1

3 0 1 0

4 1 1 0

5 1 1 1

6 1 1 0

7 1 0 1

8 1 0 0

9 1 0 1

run;

proc freq data = Have;

     table HaveAge*HaveGender/out=want1;

     table HaveAge*HaveIncome/out=want2;

     table HaveGender*HaveIncome/out=want3;

run;


Accepted Solutions
Solution
‎07-14-2014 03:52 PM
Respected Advisor
Posts: 3,777

Re: Cross Freq every var in a dataset

WAYS!

data Have;
   input ID:$2. HaveAge HaveGender HaveIncome;
   cards;
1 0 0 0
2 1 1 1
3 0 1 0
4 1 1 0
5 1 1 1
6 1 1 0
7 1 0 1
8 1 0 0
9 1 0 1
;;;;
   run;
proc summary data=have chartype;
  
class have:;
   ways 2;
  
output out=twoway;
   run;

View solution in original post


All Replies
Solution
‎07-14-2014 03:52 PM
Respected Advisor
Posts: 3,777

Re: Cross Freq every var in a dataset

WAYS!

data Have;
   input ID:$2. HaveAge HaveGender HaveIncome;
   cards;
1 0 0 0
2 1 1 1
3 0 1 0
4 1 1 0
5 1 1 1
6 1 1 0
7 1 0 1
8 1 0 0
9 1 0 1
;;;;
   run;
proc summary data=have chartype;
  
class have:;
   ways 2;
  
output out=twoway;
   run;
Frequent Contributor
Posts: 79

Re: Cross Freq every var in a dataset

As as statistician, I might warn against this for some purposes.  Programmatically, you could do the following:

data one ;
  array have_ ( 5 ) ( 5 * 1 ) ;
run ;

proc sql ;
  create table name as
  select name
  from sashelp.vcolumn
  where     libname = "WORK"
            and memname = "ONE"
  ;
quit ;

data _null_ ;
  length tables_list $ 5000 ;
  do i = 1 to &sqlobs. ;
    set name point = i ;
   do j = i + 1 to &sqlobs. ;
     want + 1 ;
     set name ( rename = ( name = name2 ))
                      point = j
                     ;
      tables_list = catx ( " "
                                  , tables_list
                                 , cat( "table "
                                        , strip( name )
                                        , "*"
                                       , strip( name2 )
                                       , "/out=want"
                                       , strip( put( want , 8. ))
                                       , ";"
                                      )
                                 ) ;
    end ;
  end ;
  call symput( "tables_list" , tables_list ) ;
  stop ;
run ;

proc freq data = one ;
  &tables_list. ;
run ;

Not sure how the formating (indenting) looks...

Of course, the only reason to take this approach in light of data_null_'s solution would be to learn about the various tools and data available.  The CLASS statement above could be replaced with:

class _all_ ;

in certain circumstances.

HTH,

Kevin

Trusted Advisor
Posts: 1,204

Re: Cross Freq every var in a dataset

proc tabulate data=have out=want(drop=_type_ _page_ _table_);

class HaveAge HaveGender HaveIncome;

table HaveAge HaveGender HaveIncome,HaveAge HaveGender HaveIncome;

run;

Super User
Posts: 17,823

Re: Cross Freq every var in a dataset

Make sure to correct for multiple testing.

http://xkcd.com/882/

Super User
Posts: 9,681

Re: Cross Freq every var in a dataset

data Have;

  input ID $2. HaveAge HaveGender HaveIncome;

call sortn(HaveGender, HaveIncome);

cards;

1 0 0 0

2 1 1 1

3 0 1 0

4 1 1 0

5 1 1 1

6 1 1 0

7 1 0 1

8 1 0 0

9 1 0 1

run;

proc freq;

................

Xia Keshan

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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