Published on :
Macro CREATION_INTERNE

Frequency Analysis on Multiple Tables with a Macro

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by creating two distinct datasets, 'males' and 'females', using DATA steps with in-line data (datalines). It then executes the PROC FREQ procedure on each table to calculate the frequency of ages by sex. The main part of the code is the definition of a '%multfreq' macro. This macro is designed to automate the execution of PROC FREQ on a list of datasets and for a specified list of variables, thus offering great flexibility for repetitive analyses. Finally, the macro is called to process the two previously created tables.
Data Analysis

Type : CREATION_INTERNE


Data for the 'males' and 'females' tables are created directly within the script using 'datalines' statements within two separate DATA steps. No external data source is required.

1 Code Block
DATA STEP Data
Explanation :
This block creates the 'males' table from internal data provided via 'datalines'. The data contains information about male individuals.
Copied!
1DATA males;
2INFILE DATALINES dlm='|' dsd missover;
3INPUT NAME : $8. SEX : $1. AGE : best32. HEIGHT : best32. WEIGHT : best32.;
4label ;
5FORMAT ;
6datalines4;
7Alfred|M|14|69|112.5
8Henry|M|14|63.5|102.5
9James|M|12|57.3|83
10Jeffrey|M|13|62.5|84
11John|M|12|59|99.5
12Philip|M|16|72|150
13Robert|M|12|64.8|128
14Ronald|M|15|67|133
15Thomas|M|11|57.5|85
16William|M|15|66.5|112
17;;;;
18RUN;
2 Code Block
DATA STEP Data
Explanation :
This block creates the 'females' table from internal data provided via 'datalines'. The data contains information about female individuals.
Copied!
1DATA females;
2INFILE DATALINES dlm='|' dsd missover;
3INPUT NAME : $8. SEX : $1. AGE : best32. HEIGHT : best32. WEIGHT : best32.;
4label ;
5FORMAT ;
6datalines4;
7Alice|F|13|56.5|84
8Barbara|F|13|65.3|98
9Carol|F|14|62.8|102.5
10Jane|F|12|59.8|84.5
11Janet|F|15|62.5|112.5
12Joyce|F|11|51.3|50.5
13Judy|F|14|64.3|90
14Louise|F|12|56.3|77
15Mary|F|15|66.5|112
16;;;;
17RUN;
3 Code Block
PROC FREQ
Explanation :
These two procedures perform a frequency analysis on the 'males' and 'females' tables, respectively. They calculate the cross-distribution of 'sex' and 'age' variables and store the results in tables 'u01' and 'u02'.
Copied!
1PROC FREQ DATA=males ;
2 tables sex*age /list missing out=u01;
3 where 1=1;
4RUN;
5
6PROC FREQ DATA=females ;
7 tables sex*age /list missing out=u02;
8 where 1=1;
9RUN;
4 Code Block
MACRO
Explanation :
Definition of the '%multfreq' macro. It takes as parameters a list of datasets ('dsnlist'), a list of frequency tables to generate ('tables'), and a 'where' condition. The macro loops through each dataset and executes PROC FREQ for each requested frequency table, creating an output dataset for each.
Copied!
1%macro multfreq(dsnlist=%str( ),tables=%str( ),where=%str());
2
3%local _dsncount _dsni _tablesi _tablescount _currentdsn _currentable;
4
5%let _dsncount=%sysfunc(countw(&dsnlist,%str( )));
6%let _tablescount=%sysfunc(countw(&tables,%str( )));
7
8%DO _dsni=1 %to &_dsncount.;
9 %let _currentdsn=%scan(&dsnlist.,&_dsni,%str( ));
10 title "&_currentdsn";
11
12 PROC FREQ DATA= &_currentdsn ;
13 %DO _tablesi=1 %to &_tablescount.;
14 %let _currenttable=%scan(&tables.,&_tablesi,%str( ));
15 tables &_currenttable. /list missing out=%scan(&_currentdsn.,-1,%str(.))_freq0&_tablesi.;
16 %END;
17 &where.;
18 RUN;
19
20%END;
21%mend multfreq;
5 Code Block
MACRO
Explanation :
Call to the '%multfreq' macro to perform frequency analysis on the 'males' and 'females' tables. The requested frequency table is the cross-distribution of 'sex' and 'age'.
Copied!
1%multfreq(
2dsnlist=%str(males females)
3,tables=%str(sex*age)
4,where=%str()
5);
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.