SAS interview questions:Macros

1. Have you used macros? For what purpose you have used? Yes I have, I used macros in creating analysis datasets and tables where it is necessary to make a small change through out the program and where it is necessary to use the code again and again. 2. How would you invoke a macro? After I have defined a macro I can invoke it by adding the percent sign prefix to its name like this: % macro name a semicolon is not required when invoking a macro, though adding one generally does no harm. 3. How can you create a macro variable with in data step? with CALL SYMPUT 4. How would you identify a macro variable? with Ampersand (&) sign 5. How would you define the end of a macro? The end of the macro is defined by %Mend Statement 6. For what purposes have you used SAS macros? If we want use a program step for executing to execute the same Proc step on multiple data sets. We can accomplish repetitive tasks quickly and efficiently. A macro program can be reused many times. Parameters passed to the macro program customize the results without having to change the code within the macro program. Macros in SAS make a small change in the program and have SAS echo that change thought that program. 7. What is the difference between %LOCAL and %GLOBAL? % Local is a macro variable defined inside a macro.%Global is a macro variable defined in open code (outside the macro or can use anywhere). 8. How long can a macro variable be? A token? A component of SAS known as the word scanner breaks the program text into fundamental units called tokens. · Tokens are passed on demand to the compiler. · The compiler then requests token until it receives a semicolon. · Then the compiler performs the syntax check on the statement. 9. If you use a SYMPUT in a DATA step, when and where can you use the macro variable? The macro variable created by the CALL SYMPUT routine cannot be used in the same datastep in which it got created. Other than that we can use the macro variable at any time.. 10. What do you code to create a macro? End one? We create a macro with %MACRO statement and end a macro with %MEND statemnt.

What is the difference between %PUT and SYMBOLGEN? %PUT is used to display user defined messages on log window after execution of a program where as % SYMBOLGEN is used to print the value of a macro variable resolved. Here is a simple example: %macro plot(yvar= . How do you add a number to a macro variable? Using %eval function or %sysevalf function if the number is a floating number. we call it as nesting of macros. %put x="&A". 14. Define it as Global. Macro parameters allow you to pass information into a macro. proc plot. Every macro's beginning is identified the keyword %macro and end with %mend. 16. Can you execute a macro within a macro? Describe. There are different ways of assigning a global variable. Use following statement to assign the value of a rather than the variable itself %Let A=xyz. If you need the value of a variable rather than the variable itself what would you use to load the value to a macro variable? If we need a value of a macro variable then we must define it in such terms so that we can call them everywhere in the program. %plot(age. %mend plot. This will assign "xyz" to x. run. Can you execute macro within another macro? If so.xvar= ).sex) 17. How would you code a macro statement to produce information on the SAS log? This statement can be coded anywhere? . 12. how would SAS know where the current macro ended and the new one began? Yes. not the variable xyz to x.11. Such macros are called nested macros. 13. I can execute macro within a macro. Simplest method is %LET. plot &yvar*&xvar. is macro variable. which is allowed. Yes. 15. in log window. How are parameters passed to a macro? A macro variable defined in parentheses in a %MACRO statement is a macro parameter. They can be obtained by using symget and call symput macros. Ex: A.

The use of %include does not actually set up a library. enclosed in quotation marks. To assign a value to a single macro variable. Data steps executes when SAS encounters a step boundary such as a subsequent data.It can be used to setting up a macro library. %LET statement. The %include statement points to a file and when it executed the indicated file (be it a full program. 18. or it can be a constant value enclosed quotation marks.html CALL SYMPUT is often used in if-then statements such as this: If age>=18 then call symput (“status”.%EVAL is a widely used yet frequently misunderstood SAS(r) macro language function due to its seemingly simple form. or run statement. else call symput (“status”. Value can be the name of a variable whose value SAS will use. value). I can then use this macro variable in later steps. is not a macro statement and is always executed in SAS.”minor”). despite its percent sign. and value is the value I want to assign to that macro variable. or a statement fragment) is inserted into the calling program at the location of the call. more abt callsymput routine can be read at: http://studysas.blogspot. How we can call macros with in data step? We can call the macro with CALL SYMPUT. But this is a least”adult”). 19.Caution: We cannot create a macro variable with CALL SYMPUT and use it in the same data step because SAS does not assign a value to the macro variable until the data step executes. Proc SQL .OPTIONS MPRINT MLOGIC MERROR SYMBOLGEN. Tell me about call symput? CALL SYMPUT takes a value from a data step and assigns it to a macro variable. 20. though it can be conditionally executed in a macro. Tell me about % include and % eval? The %include statement. the included file will usually contain one or more macro definitions. macro definition. These statements create a macro variable named &status and assign to it a value of either adult or minor depending on the variable age. is the name of a macro variable. I use CALL SYMPUT with this general form: CALL SYMPUT (“macro-variable-name”. and macro parameters. When using the %include building a macro library. . Where macro-variable-name. proc.

Syntax: %MACRO macro-name (parameter-1= . What system options would you use to help debug a macro? The SAS System offers users a number of useful system options to help debug macro issues and problems.. 24. MERROR: SAS will issue warning if we invoke a macro that SAS didn’t find.. 22. or macro quotation functions. parameter-2= .parn). Presents Warning Messages when there are misspellings or when an undefined macro is called. …… parameter-n = ). the macro processor automatically creates certain macro var. Specific options related to macro debugging appear in alphabetical order in the table below: MEMRPT: Specifies that memory usage statistics be displayed on the SAS Log. 21. MLOGIC: SAS prints details about the execution of the macros in the log.. Automatic variables for macro? Every time we invoke SAS. eg: &sysdate &sysday. %macro_name(par1.par2.However. you simply name the macro vars names in parenthesis in the %macro statement. mixed arithmetic and logical operators. when its actual argument is a complex macro expression interlaced with special characters. SERROR: SAS will issue warning if we use a macro variable that SAS can’t find. to reduce it to true or false. The results associated with using macro options are automatically displayed on the SAS Log. macro-text%. SYMBOLGEN: SAS prints the value of macro variables in log and also displays text . its usage and result become elusive and problematic. Describe the ways in which you can create macro variables? There are the 5 ways to create macro variables: %Let %Global Call Symput Proc SQl into clause Macro Parameters. %IF condition in macro is evaluated by %eval. 23. What is the maximum length of the macro variable? 32 characters long. To add the parameters to a macro. 25. %MEND macro-name.. Tell me more about the parameters in macro? Parameters are macro variables whose value you set when you invoke a macro. MPRINT: Displays SAS statements generated by macro execution are traced on the SAS Log for debugging purposes.

title1 ‘Distribution or Missing / Non-Missing Values’. %put &i &&varname&i.from expanding macro variables to the SAS Log..inverstigator). PROC SORT DATA = &DATASETNAME. %mend sortit.. vars=_numeric_). 27. proc sql noprint. format _character_ $missf. 1) Macros written to determine the list of variables in a dataset: %macro varlist (dsn). 3) Written macros for sorting common variables in various datasets %macro sortit (datasetname. vars=age gender bdate). %mend. %missrep(study. run. quit. What are the macros you have used in your programs? Used macros for various puposes. 4) Macros written to split the number of observations in a dataset .001. proc freq data=& %varlist(demo). %mend varlist. 28. %do i =1 %to &sqlobs. run. / missing. proc contents data = &dsn out = cont noprint. BY &PID &INVESTIGATOR. few of them are. %end. select distinct name into:varname1-:varname22 from cont.demog. What are SYMGET and SYMPUT? SYMPUT puts the value from a dataset into a macro variable where as SYMGET gets the value from the macro variable to the dataset. %sortit (ae.. %varlist(adverse) 2) Distribution or Missing / Non-Missing Values %macro missrep (dsn. _numeric_ missf. tables &vars. %mend missrep.

with an autocall macro. For ex: sasauto=’g:\busmeas\internal\macro\’. If you want to make sure your macro variable resolves as expected.%macro split (dsnorig. the code is in one location. desired to be used in multiple programs. What is auto call macro and how to create a auto call macro? What is the use of it? How to use it in SAS with macros? SAS Enables the user to call macros that have been stored as SAS programs. data &dsnsplit2. Although the program may contain other macros and/or open code. Unique advantage of %PUT over PUT is …you can use %PUT outside the data step whereas you can’t with PUT. set &dsnorig (obs = &obs1). run. The MAUTOSOURSE option must be set open and the SASAUTOS option should be assigned. %mend split.2) 29.Macro set-up:The fist step is to set-up a program that contains a macro. %split(sasuser. The auto call macro facility allows users to access the same macro code from multiple SAS programs. Rather than having the same macro code for in each program where the code is required. How to use %PUT: %let program=AE.admit4.admit5. run. 30. What it does is it writes text and values of macro variable after execution to the SAS System LOG. %put program Name here as &program. obs1). data &dsnsplit1. dsnsplit1. dsnsplit2. you can make sure it with %PUT statement. . set &dsnorig (firstobs = %eval(&obs1 + 1)). Set MAUTOSOURSE and SASAUTOS: Before one can use the autocall macro within a SAS program. The SASAUTOS option tells SAS where to look for the macros. The MAUTOSOURSE option indicates to SAS that the autocall facility is to be activated. it is advised to include only one macro. This permits faster updates and better consistency across all the programs. Why and How to Use %PUT Statement: %Put statement is similar to the PUT statement in data step.admit.

to execute same code n number of times. ... %macro doit. (ex: &sysdate. then the loop creates a macro variable list of &var1 &var2 &var3 &var4 &var5 &var6 &var7 which further get resolved to patid sex age ethnic race wt ht You can always use Macro debugging option SYMBOLGEN to see how each macro variable got resolved. but here is the simple technique you can use to know. &&var&i %end. (couldn’t be able use those macro variables outside the particular data step) %PUT _user_: It prints macro variables that are created by the user in each environment. local. %do i=1 %to &nvars. %PUT _automatic_: It prints all the SAS defined automatic macro variables in the log... %PUT _all_: It prints all macro variables in the log that are available in all environments (global.... So if the nvars value is 7.Above %put statement resolves to … %put Program Name here as AE. %mend doit. ex: We generally use &&var&i or &&dsn&i these macro variables when we are using a %do loop. 31) How to know how &&var&i or &&dsn&i resolves? It is very confusing some times to tell rightaway how &&var&i or &&dsn&i get resolved. %PUT _local_: It prints macro variables that are created by the user and available only in the local environment. What can you do with %PUT: Numerous options are available for the %PUT statement.. %syserr etc) %PUT _global_: It prints macro variables that are created by the user and available in all environments... user and automatic). You have a dataset and it has 5 variables . &systime ..%sysdsn. Patid sex age ethnic race wt ht..

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.