Published on :
Statistical CREATION_INTERNE

Macro AHGstats_vertical - Calculation and Formatting of Statistics

This code is also available in: Deutsch Español Français
Awaiting validation
This macro automates the calculation of statistics (by default: n, mean, median, min, max) via PROC MEANS after data sorting. It handles result formatting via delimitation parameters. The code contains conditional blocks for horizontal or vertical output (based on an '&orie' variable not declared as a parameter). Note: The code block for vertical orientation contains obvious syntax errors (SQL syntax in a DATA step). The macro depends on several other utility macros (AHGwords, AHGdatasort, etc.).
Data Analysis

Type : CREATION_INTERNE


The macro works on the dataset passed as a parameter (&dsn). It generates output tables (&out).

1 Code Block
MACRO
Explanation :
Initialization of local variables and data preparation. Calls to external utility macros (AHGdatasort, AHGwords, AHGgettempname) to sort data and parse the requested statistics list and their associated formats.
Copied!
1%local statN single %AHGwords(mystat,20)
2 %AHGwords(myformat,20) %AHGwords(IsStat,20);
3%local i sortdsn mystats;
4 
5%AHGgettempname(sortdsn);
6 
7%AHGdatasort(DATA = &dsn,out=&sortdsn, BY =&bigby &BY);
8%DO i =1 %to %AHGcount(&stats);
9 %let single=%scan(&stats,&i,%str( ));
10 %let isStat&i=0;
11 %IF not %index(&single,%str(%22)) %THEN
12 %DO;
13 %let isStat&i=1;
14 %let mystats=&mystats &single ;
15 %END;
16%END;
17 
18%put I am fine out=&out;
19%AHGsetstatfmt(statfmt=&mystats);
20%let statN=%AHGcount(&stats);
21 
22%DO i=1 %to &statN;
23 %let single=%scan(&stats,&i,%str( ));
24 %let mystat&i=%scan(&single,1,&split);
25 %let myformat&i=%scan(&single,2,&split);
26 %IF %AHGblank(&&myformat&i) and %str(&&isStat&i) %THEN %let myformat&i=&&&&formatof&&mystat&i;
27 %IF &&isStat&i %THEN %AHGpm(mystat&i myformat&i);
28%END;
29%put I am fine 2;
2 Code Block
PROC MEANS Data
Explanation :
Calculation of requested descriptive statistics using PROC MEANS. Results are stored in the table defined by &out.
Copied!
1PROC MEANS DATA=&sortdsn noprint alpha=α;
2 var &var;
3 OUTPUT out=&out
4 %DO i=1 %to &statN;
5 %IF &&isStat&i %THEN &&mystat&i%str(=)&&mystat&i;
6 %END;
7 ;
8 %IF not %AHGblank(&bigby&BY) %THEN BY &bigby &BY;;
9RUN;
3 Code Block
DATA STEP Data
Explanation :
Post-processing step to apply conditional formats or rename grouping values (by variables) according to the &byfmts parameter.
Copied!
1DATA &out;
2 SET &out;
3 
4 %DO i=1 %to %AHGcount(&byfmts,dlm=&split);
5 FORMAT ahuigeby $50.;
6 IF %AHGequaltext(INPUT(&BY,$100.),"%scan(%scan(&byfmts,&i,&split),1,&split2)")
7 THEN ahuigeby="%scan(%scan(&byfmts,&i,&split),2,&split2)";
8 %END;
9RUN;
4 Code Block
PROC SQL Data
Explanation :
If the (undeclared) &orie variable is 'hori', a PROC SQL formats the statistics using the PUT function with the previously identified formats.
Copied!
1%IF &orie=hori %THEN
2 %DO;
3 PROC SQL;
4 %IF not &PRINT %THEN create TABLE &out as ;
5 select
6/* %if not %AHGblank(&bigby) %then %str(&bigby,);*/
7 ahuigeby/* %sysfunc(tranwrd(&by,%str( ),%str(,)))*/
8 %DO i=1 %to %AHGcount(&stats);
9 %IF &&isStat&i %THEN ,put(&&mystat&i, &&myformat&i) as &&mystat&i ;
10 %ELSE ,&&mystat&i;
11 %END;
12 from &out
13 ;QUIT;
14 
15 %END;
5 Code Block
DATA STEP Data
Explanation :
If &orie is 'vert', an attempt is made to create an 'ahuigeprint' table. WARNING: This block contains invalid code. It mixes DATA STEP syntax ('set &out') with SQL clauses ('put(...) as ...', 'from &out'). This code will fail at execution.
Copied!
1%IF &orie=vert %THEN
2 %DO;
3 
4 DATA ahuigeprint;
5 SET &out ;
6 %DO i=1 %to %AHGcount(&stats);
7 %IF &&isStat&i %THEN ,put(&&mystat&i, &&myformat&i) as &&mystat&i ;
8 %ELSE ,&&mystat&i;
9 %END;
10 from &out
11 ;QUIT;
12 
13 %END;
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.