%Macro FreqAll( Lib= Work , Mem= , Vars= , LevelLimit= , NObs2View= , Format= Yes , Order= Internal , DataOut= , ExcludeVars= , ) ; /*****************************************************************************/ /** Macro Name : FreqAll **/ /** Taken from the 2008 SAS Global Forum Proceedings paper "Start Im[poriving Your Data Quality with %FreqAll by Toby Dunn, Dunn Consulting. **/ /** Purpose : The FreqAll macro creates a customized frequency listing **/ /** of specified vars. **/ /** Parameters : Lib ~ Libname of the Data Set containing the data to run **/ /** the frequencies on. Default value is 'WORK'. **/ /** Mem ~ Name of the Input Data Set. No Defualt Value. **/ /** Vars ~ Space Seperated List of variables to perform the **/ /** Frequency On. **/ /** Valid values are: _All_ - All Vars **/ /** _Numeric_ - Numeric Vars Only **/ /** _Character_ - Character Vars Only**/ /** User Supplied List of Var Names **/ /** LevelLimit ~ Numeric Limit At Which Frequecy For A Variable**/ /** Will Be Truncated To The Level Specified In **/ /** NObs2View Parameter. **/ /** NObs2View ~ A number representing the Top and Bottom **/ /** frequencies to be included in the report. **/ /** If no value is specified then all of the **/ /** frequency values will be included in the **/ /** output Data Set. No default value. **/ /** Format ~ Specifies whether to print the frequency **/ /** values by their associated formats. **/ /** Valid Values are 'Yes','No',’N’, and ’Y’. The **/ /** Default Value is 'YES'. **/ /** Order ~ Specifies how SAS should group the data in the **/ /** frequency analysis. **/ /** Valid Values: Internal, Data, Formatted, Freq **/ /** Default value is Internal. **/ /** DataOut ~ The output data set name that will store the **/ /** Frequencies. **/ /** This data set should either not exist or should be "zeroed out" before running the app since it gets written to via proc appemd. **/ /** ExcludeVars ~ Space Seperated List Of Valid Variable Names **/ /** To Exclude From The Frequency Output. **/ /** User Defined List. **/ /** ***************** **/ /** * Make It Right * **/ /** ***************** **/ /*****************************************************************************/ %Local DSID Close DoWindow ; %If ( %Length( %SuperQ(Vars) ) > 0 ) %Then %Do ; %Let Vars = %SysFunc( Compbl( &Vars ) ) ; %End ; %If ( %Length( %SuperQ(ExcludeVars) ) > 0 ) %Then %Do ; %Let ExcludeVars = %SysFunc( Compbl( &ExcludeVars ) ) ; %End ; /**********************************************/ /** Check The Existence Of The Input DataSet **/ /**********************************************/ %If ( %Sysfunc( Exist( &Lib..&Mem ) ) Eq 0 ) %Then %Do ; %Put ; %Put ; %Put ERROR: The Data Set [&Lib..&Mem] Does Not Exist!!! ; %Put ERROR: Please Check The Value Of Lib= or Mem= Parameters ; %Put ; %Put ; %Return ; %End ; /********************************************/ /** Check The Value Of Parm Vars To Ensure **/ /** That It Meets One Of The Required Types**/ /********************************************/ %If ( %SysFunc( IndexW( _ALL_ _CHARACTER_ _NUMERIC_ , %UpCase( &Vars ) ) ) EQ 0 ) Or ( %Length( %SuperQ(Vars) ) = 0 ) %Then %Do ; %Put ; %Put ; %Put ERROR: (&Vars) Is Not A Valid Value For Parameter Vars ; %Put ERROR: The Valid Values Are : _All_, _Character_, _Numeric_, Or ; %Put ERROR: A User Supplied List Of Valid Variable Names ; %Put ; %Put ; %Return ; %End ; /***********************************************/ /** Check The Value Of Parm Format To Ensure **/ /** That It Is One Of The 4 Acceptable Values **/ /***********************************************/ %If ( %SysFunc( IndexW( YES Y NO N , %UpCase( &Format ) ) ) EQ 0 ) Or ( %Length( %SuperQ(Format) ) = 0 ) %Then %Do ; %Put ; %Put ; %Put ERROR: (&Format) Is Not A Valid Value For Parameter Format ; %Put ERROR: The Valid Values Are : Yes, Y , No , N ; %Put ; %Put ; %Return ; %End ; /***********************************************/ /** Check The Value Of Parm Order To Ensure **/ /** That It Is One Of The 4 Acceptable Values **/ /***********************************************/ %If ( %SysFunc( IndexW( INTERNAL FORMATTED DATA FREQ , %UpCase( &Order ) ) ) EQ 0 ) Or ( %Length( %SuperQ(Order) ) = 0 ) %Then %Do ; %Put ; %Put ; %Put ERROR: (&Order) Is Not A Valid Value For Parameter Order ; %Put ERROR: The Valid Values Are : Internal, Formatted, Data, Or Freq ; %Put ; %Put ; %Return ; %End ; /**************************************************/ /** Check To See If A Variable Listed In Either **/ /** The Vars Or ExcludList Parameters Are In The **/ /** Specified Input Data Set. **/ /**************************************************/ %Let DSID = %SysFunc( Open( &LIB..&MEM , IS ) ) ; %If ( &DSID = 0 ) %Then %Do ; %Put ; %Put ; %Put ERROR: Error Trying To Open Data Set ( %UpCase(&DataIn) ) ; %Put ERROR: %SysFunc( SysMsg( ) ) ; %Put ; %Put ; %Return ; %End ; /*********************/ /** Check Parm Vars **/ /*********************/ %If ( %SysFunc( IndexW( _ALL_ _CHARACTER_ _NUMERIC_ , %UpCase( &Vars ) ) ) EQ 0 ) %Then %Do ; %Let VarStop = %Eval( %SysFunc( CountC( %Str(&Vars) , %Str( ) ) ) + ( %Length( &Vars ) > 0 ) ) ; %Do I = 1 %To &VarStop ; %If %Eval( %SysFunc( VarNum( &DSID , %Scan( %Str(&Vars) , &I , %Str( ) ) ) ) = 0 ) %Then %Do ; %Put ; %Put ; %Put ERROR: Can Not Find The Following Variable ( %UpCase( %Scan( %Str(&Vars) , &I , %Str( ) ) ) ) ; %Put ERROR: In The DataSet &LIb..&Mem ; %Put ERROR: Please Specify A Variable In &Lib..&Mem ; %Put ; %Put ; %Return ; %End ; %End ; %End ; /****************************/ /** Check ExcludeList Parm **/ /****************************/ %If ( %Length( %SuperQ(ExcludeVars) ) > 0 ) %Then %Do ; %Let ExlStop = %Eval( %SysFunc( CountC( %Str(&ExcludeVars) , %Str( ) ) ) + ( %Length( &ExcludeVars ) > 0 ) ) ; %Do K = 1 %To &ExlStop ; %If %Eval( %SysFunc( VarNum( &DSID , %Scan( %Str(&ExcludeVars) , &K , %Str( ) ) ) ) = 0 ) %Then %Do ; %Put ; %Put ; %Put ERROR: Can Not Find The Following Variable (%UpCase( %Scan( %Str(&ExcludeVars) , &K , %Str( ) ) ) ) ; %Put ERROR: In The DataSet %UpCase(&LIb..&Mem) ; %Put ERROR: Please Specify A Variable In %UpCase(&Lib..&Mem) ; %Put ; %Put ; %Return ; %End ; %End ; %End ; %Let Close = %SysFunc( Close( &DSID ) ) ; %If ( &Close Ne 0 ) %Then %Do ; %Put ; %Put ; %Put ERROR: Error Trying To Close Data Set ( %UpCase(&DataIn) ) ; %Put ERROR: %SysFunc( SysMsg( ) ) ; %Put ; %Put ; %Return ; %End ; /*******************************************/ /** Check That The Values Of The Format **/ /** And Order Parameters To Not Logically **/ /** Conflict. **/ /*******************************************/ %If ( ( %SysFunc( IndexW( NO N , %UpCase( &Format ) ) ) > 0 ) And ( %Upcase( &Order ) = FORMATTED ) ) %Then %Do ; %Put ; %Put ; %Put WARNING: You Have Specified Parm Format = (&Format) And Order = (&Order) ; %Put WARNING: NonFormatted Values Will Be Used ; %Put ; %Put ; %End ; /***********************************************/ /** Check That The Values Of The LevelLimit **/ /** And NObs2View Parameters To Not Logically **/ /** Conflict. **/ /***********************************************/ %If ( ( %Length( %SuperQ(LevelLimit) ) = 0 ) And ( %Length( %SuperQ(NObs2View) ) Ne 0 ) ) Or ( ( %Length( %SuperQ(LevelLimit) ) Ne 0 ) And ( %Length( %SuperQ(NObs2View) ) = 0 ) ) Or ( &LevelLimit < &NObs2View ) %Then %Do ; %Put ; %Put ; %Put WARNING: You Have Specified Parm LevelLimit = (&LevelLimit) And NObs2View = (&NObs2View) ; %Put WARNING: All Levels Will Be Created ; %Put ; %Put ; %End ; /*******************************/ /** Create Frequency Data Set **/ /*******************************/ ODS Listing Close ; ODS Output clear; *<---jgb, 12/13/2010 8:17:21 AM---; ODS Output OneWayFreqs = Freqs ; Proc Freq Data = &Lib..&Mem %If ( %Length( %SuperQ( ExcludeVars ) ) > 0 ) %Then %Do ; ( Drop = &ExcludeVars ) %End ; Order = &Order ; Table &Vars / Missing ; %If ( ( %UpCase( &Format = NO ) ) Or ( %UpCase( &Format ) = N ) ) %Then %Do ; Format _All_ ; %End ; Run ; ODS Listing ; /*********************************/ /** Create Final Data Structure **/ /*********************************/ Data NewFreqsA ( Keep = Var Val Frequency Percent Level ) ; Set Freqs ; By Table NotSorted ; If First.Table Then Level = 0 ; Level + 1 ; Var = PropCase( Compress( Scan( Table , 2 , ' ' ) ) ) ; Val = Strip( VValueX( Var ) ) ; Run ; /*********************************************/ /** Attach Variable Attributes And Truncate **/ /** Data If LevelLimit And NObs2View Are **/ /** Specified. **/ /*********************************************/ Proc SQL ; Create Table NewFreqsB As Select Freqs.* , CatX( ' ' , PropCase( Freqs.Var ) , PropCase( Dict.Attr ) ) As Attribute From NewFreqsA As Freqs Left Join ( Select Name , ( PropCase( Type ) || ':' || PropCase( Strip( Put( Length , 8. -L ) ) ) || ' ' || PropCase( Label ) ) As Attr Format=$50. From Dictionary.Columns Where LibName = %UpCase( "&Lib" ) And MemName = %Upcase( "&Mem" ) ) As Dict On Freqs.Var = Dict.Name %If ( %Length( %SuperQ( LevelLimit ) ) > 0 ) And ( %Length( %SuperQ( NObs2View ) ) > 0 ) %Then %Do ; Group By Freqs.Var Having ( Max( Freqs.Level ) < &LevelLimit ) Or ( ( Freqs.Level <= &Nobs2View ) Or ( Freqs.Level > ( Max( Freqs.Level ) - &Nobs2View ) ) ) %End ; Order By Freqs.Var , Freqs.Level ; Quit ; /*********************/ /** Output Data Set **/ /*********************/ Proc Append Base = &DataOut Data = NewFreqsB Force ; Run ; /*************************/ /** Clean Up Work Space **/ /*************************/ Proc Datasets NoList ; Delete NewFreqsA NewFreqsB Freqs ; Quit ; %Mend FreqAll ;