80 likes | 96 Views
Learn about the three types of macro expressions—text, logical, and arithmetic—used in SAS programming. Explore examples, language elements, and operators to enhance your understanding and proficiency in macro programming.
E N D
Macro Expressions • There are three types of macro expressions: text, logical, and arithmetic. • A text expression is any combination of text, macro variables, macro functions, or macro calls. • Text expressions are resolved to generate text. Here are some examples of text expressions: • &BEGIN • %GETLINE • &PREFIX.PART&SUFFIX • %UPCASE(&ANSWER)
Macro Expressions • Logical expressions and arithmetic expressions are sequences of operators and operands forming sets of instructions that are evaluated to produce a result. • An arithmetic expression contains an arithmetic operator. • A logical expression contains a logical operator.
Macro Language Elements that Evaluate Arithmetic and Logical Expressions • %DO macro-variable=expression %TO expression<%BY expression>; • %DO %UNTIL (expression); • %DO %WHILE (expression); • %EVAL (expression); • %IF expression %THEN statement; • %QSCAN (argument, expression<,delimiters>) • %QSUBSTR (argument, expression<,expression>) • %SCAN (argument, expression,<delimiters>) • %SUBSTR (argument, expression<,expression>) • %SYSEVALF (expression, conversion-type)
Example %let A=2; %let B=5; %let operator=+; %put The result of &A &operator &B is %eval(&A &operator &B).; When you submit these statements, the %PUT statement writes this line to the log: The result of 2 + 5 is 7.
Examples %let a=%eval(1+2); %let b=%eval(10*3); %let c=%eval(4/2); %let i=%eval(5/3); %put The value of a is &a; %put The value of b is &b; %put The value of c is &c; %put The value of I is &i; When you submit these statements, the following messages appear in the log: The value of a is 3 The value of b is 30 The value of c is 2 The value of I is 1 • %let d=%eval(10.0+20.0); /*INCORRECT*/ • Because the %EVAL function supports only integer arithmetic, the macro processor does not convert a value containing a period character to a number, and the operands are evaluated as character operands. This statement produces the following error message: • ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 10.0+20.0
Examples %let a=%sysevalf(10.0*3.0); %let b=%sysevalf(10.5+20.8); %let c=%sysevalf(5/3); %put 10.0*3.0 = &a; %put 10.5+20.8 = &b; %put 5/3 = &c; • The %PUT statements display the following messages in the log: 10.0*3.0 = 30 10.5+20.8 = 31.3 5/3 = 1.6666666667 • When the %SYSEVALF function evaluates arithmetic expressions, it temporarily converts the operands that represent numbers to floating point values.
Examples • The %SYSEVALF function provides conversion type specifications: BOOLEAN,INTEGER, CEIL, and FLOOR. • For example, the following %PUT statements return 1, 2, 3, and 2 respectively: %let a=2.5; %put %sysevalf(&a,boolean); %put %sysevalf(&a,integer); %put %sysevalf(&a,ceil); %put %sysevalf(&a,floor);