SAS SCREEN CONTROL LANGUAGE: a GENTLE INTRODUCTION Jean Hardy Services Conseils Hardy Inc
Total Page:16
File Type:pdf, Size:1020Kb
Interactive systems 319 SAS SCREEN CONTROL LANGUAGE: A GENTLE INTRODUCTION Jean Hardy Services Conseils Hardy Inc. ABSTRACT • computed variables created by SCL; • system variables; SAS users that become acquainted with SAS Screen • MACRO variables. Control Language are often discouraged. What appears as a formidable task, namely learning this new language Unlike DATA step flow, statements of an SCL program and its intricacies, can be made easier by concentrating are not executed top-dawn; some execute once as the on the fundamental statements and features. first observation is displayed while others can execute many times for each observation in the SAS dataset. This paper is a tutorial to the Screen Control Language. Controlling execution flow is a major challenge in some We don't like to teach SCL in the context of SASIAF, SCL applications. intimidating by itself. We prefer to see it from within SASIFSP, disregarding details about PROC FSEDIT. STRUCTURE OF AN SCL PROGRAM Topics covered include the use of labels, field statements and functions, error trapping, use of dataset and observa Each FSEDIT and FSBROWSE screen include an SCL tion functions, and useful programming and debugging program that is stored with it; this program is empty by tips. default. For example, an SCL program validating the SAS dataset PERM.RESULT is created by running This paper is aimed toward intermediate level SAS users, FSEDIT with a valid screen name: new or novice in their knowledge of SCL. Those that are discouraged, disenchanted or skeptical about SCL are LIBNAME PERM 'file-or-direccory'; PROC FSEDIT DATA=PERM.RESULT especially welcomed. SCREEN=PERM.CAT.ED1.SCREEN; VAA FRENCHl FRENCH2 FRENCH3; WHAT IS SCL RUNj SCL is a programming language, independent but +FSEDIT PERM.RESULT--------------------------- looking often very much like DATA step language. It is ICommand ===> 1 aimed to the development of interactive full-screen 1 applications. In the SASIFSP FSEDIT procedure, an 1 FRENCH1: 13 SCL program can be used for example to: 1 1 FRENCH2: 12 1 + validate fields using hard-coded values; 1 FRENCH3: 15 + cross-validating fields or validating using an external 1 1 table (a SAS dataset); 1 + explain to the user what the application is currently doing; Figure 1. Base screen and frrst observation + manage additional windows (value lists, etc.). To write an SCL program, use the MOD command The fundamental components of SCL language are: followe by choice 3 "Edit Program Statements and Com pile" - you then see the following window (figure 2): + programming statements (like ARRAY or IF); • functions and routines (like SUM or LEFf); +FSEDIT Program-------------------------------- • variables (various types) . ICommand ===> 1 100001 The SCL and DATA step homonymous statements play 100002 the same role; only a subset of the current DATA step 100003 statements are available. Functions convey most of the 100004 power of SCL; the DATA step homonymous functions Figure 2. SCL programming window share a common syntax. Four types of variables are available: An SCL program in SASIFSP is nade if at least five different blocks having reserved names - each of them • screen variables, from the dataset currently edited; terminated by a RETURN statement: NESUG '92 Proceedings 320 Interactive systems FSEINlT: + SUM is the SCL sum statement; statements executed once, when beginning FSEDIT + PUT is an SCL function converting a numeric or FSBROWSE, before the fust observation is value to a character string; displayed. Typical use is to open secondary datasets + RETIJRN statement indicates the end of the block. or initialize constants and variables; Compilation of the program starts when the user issues !NIT: the END command from the program window. If the statements executed before each observation is program is error-free, it is stored in the SAS catalog as displayed. Typical use include initializing variables a part of the screen entry; the modification screen is computed for each observation or displaying initial redisplayed. An other END command give the control messages when a new observation is shown; back to the screen and enable editing data. MAIN: The structure of the SAS dataset PERM.RESULT is not statements executed each time the user modify a modified by the SCL program: the S variable computed field and press <RETI1RN:> or a function key. If by SCL is not added to the dataset. If we enter the fields does not match defined attributes (minimum, values 20, 22 et 30, the SCL program produce: maximum, required, etc.), an error is issued and the MAIN block is not executed. Typical use are +FSEDIT PERM.RESULT--------------------------- performing extensive validation and cross-validation, ICommand ===> computing new variables or displaying error or ITotal: 72 warning messages specific to fields; I I FRENCH1: 20 I 1ERM: I FRENCH2, 22 executed when leaving an observation. Typical use I I FRENCH3: 30 include final validation or update of a secondary file; I I FSETERM: I statements executed only once, when leaving the Figure 4. Behavior of the SCL program from figure 3. FSEDIT or FSBROWSE procedure. Typical use are exporting content of MACRO valrables or closing ADDING FIELDS TO THE SCREEN secondary files. It is possible to add to the screen some fields, reflecting Figure 3 illustrates a basic SCL program: the content of corresponding SCL variables: +FSEDIT Program-------------------------------- + use the MOD command and select choice 2 "Screen ICommand ===> Modification and Field Identification", I + place the field on the screen using underline charac 100001 FSEINIT: 100002 return; ters (" _") and add a description to the field, 100003 + issue the END command and answer Y (Yes) to the 100004 INIT: question "Did you created computational fields?", 100005 return; 100006 + provide the attributes of the added field, and issue 100007 MAIN: the END command, 100008 s=sum(french1,french2,french3); 100009 _msg_="Tocal:" II puc (s, 5.); + when asked to, place the cursor on the field and 100010 return; press <ENTER> to confrrm its exact position. 1000ll 100012 TERM: 100013 return; After these steps, the field reflect whatever content is in 100014 the SCL variable. Let's suppose that the TOTAL field is 100015 FSETERM: added to the screen (figure 5). We also modify the SCL 100016 return; program (figure 6) to put the sum in the TOTAL Figure 3. A basic SCL program variable, computing it only if the number of valid values (X) in FRENCHl, FRENCH2 and FRENCH3 is + FRENCH!, FRENCH2 et FRENCH3 are screen larger than l; if not, the ..MSG_ variable display a variables from the PERM.RESULT SAS dataset; warning explaining why the computation was not done. + S is an SCL computed variable; In version 6.06, computing the variable X can be made + ..MSG_ is a reserved SCL variable automatically simpler: displayed under the command line, in the message zone, after the assignment of a value; X=N(FRENCH1,FRENCH2,FRENCH3); NESUG 192. Proceedings Interactive Systems 321 TOTAL=SUM(FRENCHl,FRENCH2,FRENCH3); +FSEDIT PERM.RESULT--------------------------- ICommand ===> I The sum is computed ~ displaying the window and I is not computed again following modifications: not very I FRENCH1: 13 useful. Now what happens if the same statement is I I FRENCH2 : 12 placed in both !NIT and MAIN blocks? The sum is I computed before displaying the window as well as after I FRENCH3: 15 any modification (figures 9 and 10). A message will also I ================== I TOTAL: be displayed when user move to a new observation: I "Enter data except for TOTAL which is computed". Figure 5. Modified data entry screen. 100004 INIT: 100005 -ffiS9_='Enter data except for TOTAL whi 100006 100006 total=sum(frenchl,french2,french3J; 100007 MAIN: 100007 return; 100008 x=(frenchl ne .J+(french2 ne .J+ 100008 100009 (french3 ne .J; 100009 MAIN: 100010 if x>l then total= 100010 total=sum(frenchl.french2.french3J; 1000ll sum(frenchl,french2,french3J; 100011 return: 100012 else -ffiS9_='Insufficient data'; 100012 100013 return: 100014 Figure 9. Computing in 2 blocks (some lines truncated) 100015 TERM: Figure 6. Computing TOTAL conditionnally. +FSEDIT PERM.RESOLT--------------------------- ICommand ===> Figure 7 shows the total computed with two or more I Enter data except: for TOTAL which is computed I valide values; in figure 8, only one valid value is entered I FRENCH 1 : 13 and the warning message is displayed: I I FRENCH2: 12 I +FSEDIT PERM.RESULT--------------------------- I FRENCH3: 15 ICommand ===> I ================== I I TOTAL: 40 I I I FRENCH 1 : 20 I Figure 10. Effect of the INIT block. I FRENCH2 : 22 I I FRENCH3 : 30 Would we add something by computing TOTAL in the I ================== TERM block? No because TOTAL is not a variable in I TOTAL: 72 the SAS dataset. If TOTAL was in the dataset, compu I ting TOTAL again in TERM would be wise. Figure 7. Entry of two values or more. ADVANCED VALIDATION +FSEDIT PERM.RESULT--------------------------- Let's add the character variable SEX to the screen: ICommand ===> IInsufficient data PROC FSEDIT DATA=PERM.RESULT I SCREEN=PERM.CAT.ED4.SCREEN: I FRENCH1: VAR SEX FRENCHl FRENCH2 FRENCH3; I I FRENCH2: n RUN; I I FRENCH3: +FSEDIT PERM.RESULT-------------------------- I ================== ICommand ===> I TOTAL: I I I I SEX: F Figure 8. Entry of a single value. I I FRENCH1: 13 USING I OTHER BLOCKS I FRENCH2: 12 I In Sa." the position of statements in one block or the I FRENCH3: 15 I ================== other is a major concern. What happens if the previous I TOTAL: program contains, only in the !NIT block, the following statement: Figure 11. Data entry screen with SEX added. NESUG '92 Proceedings 322 Interactive systems When a field is validated, SCI.. can issue an error over modified would be a nice addition; never surprise the a field if data are unacceptable. The user will not be user with an action he has to figure by himself.