Published on :
Macro EXTERNE

Windowing Macro for PROC PRINT

This code is also available in: Deutsch Español Français
This script first defines a series of modal windows with PROC PMENU and %WINDOW to create a user interface. The %printwindow macro then orchestrates the interaction: it asks the user to enter a data set name, validates its existence, asks for an (optional) list of variables and validates their presence, then asks for formatting options for PROC PRINT (number of observations, suppression of numbers, etc.). Finally, it dynamically assembles and executes a PROC PRINT step based on all collected information.
Data Analysis

Type : EXTERNE


The script is designed to operate on any SAS data set whose name is interactively provided by the user via an input window. The existence of the data set is validated at runtime.

1 Code Block
PROC PMENU
Explanation :
This procedure defines a menu structure named 'exit' in the orion.menus catalog. This menu is used by interactive windows to offer exit options ('OK', 'Cancel') and associates actions with these selections.
Copied!
1PROC PMENU catalog=orion.menus;
2 menu exit;
3 item 'Exit' menu=x;
4 
5 menu x;
6 
7 item 'OK' selection=y;
8 item 'Cancel' selection=z;
9 
10 selection y 'end';
11 selection z 'command focus';
12 
13QUIT;
2 Code Block
%WINDOW
Explanation :
Declares four modal windows (dsn, var, opt, err) that will serve as a user interface for parameter input. Each window has a specific layout to display text and input fields for the data set name, variables, printing options, or to display error messages.
Copied!
1%let msg=Press ENTER to continue.;
2 
3%window dsn columns=80 rows=20 menu=orion.menus.exit
4 
5 #3 @ 6 'Data Set: ' dsn 41 attr=underline required=yes
6 #5 @ 6 'Press ENTER to continue.' msg protect=yes;
7 
8%window var columns=80 rows=20 menu=orion.menus.exit
9 
10 #3 @ 6 'Data Set: ' dsn 41 attr=underline protect=yes
11 #5 @ 6 'Variables: ' var 41 attr=underline
12 #7 @ 6 'Press ENTER to continue.' msg protect=yes;
13 
14%window opt columns=80 rows=20 menu=orion.menus.exit
15 
16 # 3 @ 6 'Data Set: ' dsn 41 attr=underline protect=yes
17 # 5 @ 6 'Variables: ' var 41 attr=underline protect=yes
18 # 7 @ 6 '# of obs: ' obs 2 attr=underline
19 # 9 @ 6 'Suppress Obs #s (Y or N): ' sup 1 attr=underline
20 #10 @ 6 'Double Space (Y or N): ' dbl 1 attr=underline
21 #11 @ 6 'Column Labels (Y or N): ' lab 1 attr=underline
22 #14 @ 6 'Press ENTER to continue.' msg protect=yes;
23 
24 %window err columns=80 rows=20 menu=orion.menus.exit
25 
26 #3 @ 6 'Data Set ' c=red dsn p=yes c=red attr=rev_video
27 ' does not exist.' c=red
28 
29 #5 @ 6 'Enter Y to try again or N to stop: '
30 try 1 attr=underline
31 
32 #7 @ 6 'Press ENTER to continue.' msg protect=yes;
3 Code Block
Macro
Explanation :
The core of the script. This macro manages the interactive flow: it displays windows to collect user input, validates the existence of the data set and variables using %sysfunc functions, dynamically builds the options and VAR statement for PROC PRINT, and executes the report.
Copied!
1%macro printwindow;
2 
3 %local dsn var vars dsid rc obs sup dbl lab supx dblx labx try msg;
4 %let msg=Press ENTER to continue.;
5 %let sysmsg=Enter dataset name.;
6 %display dsn;
7 %let dsn=%upcase(&dsn);
8 
9 %DO %while(%sysfunc(exist(&dsn))=0);
10 %let dsn=%upcase(&dsn);
11 %let try=;
12 %display err;
13 %IF %upcase(&try)=Y %THEN %display dsn;
14 %ELSE %DO;
15 %put ERROR: Dataset &dsn does not exist.;
16 %return;
17 %END;
18 %END;
19 
20 %let sysmsg=Enter variable names or leave blank.;
21 %display var;
22 
23 %IF &var ne %THEN %DO;
24 %let dsid=%sysfunc(open(&dsn(keep=&var)));
25 %let rc=%sysfunc(close(&dsid));
26 %let var=%upcase(&var);
27 %IF &dsid=0 %THEN %DO;
28 %put ERROR: Variables(&var) not in &dsn..;
29 %return;
30 %END;
31 %let vars=var &var;
32 %END;
33 
34 %let sysmsg=Select options.;
35 %display opt;
36 
37 %IF &obs ne %THEN %let obs=(obs=&obs);
38 %IF %upcase(&sup)=Y %THEN %let supx=noobs;
39 %IF %upcase(&dbl)=Y %THEN %let dblx=double;
40 %IF %upcase(&lab)=Y %THEN %let labx=label;
41 
42 PROC PRINT DATA=&dsn &obs &supx &dblx &labx;
43 &vars;
44 title "&dsn";
45 RUN;
46 title;
47 
48 %put NOTE: Processing complete.;
49 
50%mend printwindow;
4 Code Block
Macro Call
Explanation :
Executes the %printwindow macro, thereby initiating the interactive report generation process.
Copied!
1%printwindow
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.