2013年9月19日星期四

Notes for SAS Advanced Programmer_CH10

CH10

1. Remember that when both macro language statements and SAS language statements occur in the same step, the macro processor executes macro language statements before any SAS language statements are executed.

Because the %LET statements are always processed by the macro processor
before the DATA step is executed, the value of foot will always be whatever the last
%LET statement assigns.

2. When you use the SYMPUT routine to create a macro variable in a DATA step, the
macro variable is not actually created and assigned a value until the DATA step is
executed. Therefore, you cannot successfully reference a macro variable that is created
with the SYMPUT routine by preceding its name with an ampersand within the same
DATA step in which it is created.

3. To use a literal with the SYMPUT routine, you enclose the literal string in quotation
marks.
CALL SYMPUT(‘macro-variable’, ‘text’);

4. example:   call symput(’foot’,’Some Fees Are Unpaid’);

5.
values of numeric variables are automatically converted to character values, using
the BEST12. format.

6. The SYMPUTX routine is very similar to the SYMPUT routine. In addition to
creating a macro variable and assigning a value to it, the SYMPUTX routine also
automatically removes leading and trailing blanks from both arguments.

7. CALL SYMPUTX(macro-variable,expression);
e.g.: call symputx(’crsname’,course_title);
call symputx(’date’,put(begin_date,mmddyy10.));
call symputx(’due’,put(fee*(total-paidup),dollar8.));

8. 另一种方法来用call symput(macrovariableName, expression)
e.g.: call symput(’numpaid’,trim(left(paidup)));
call symput(’numstu’,trim(left(total)));
call symput(’crsname’,trim(course_title));

9. You can use the PUT function to
􀀀 perform explicit numeric-to-character conversions
􀀀 format the result of a numeric expression.


10.

11. In this example, you use one call to the SYMPUT routine in order to create one
macro variable for each value of the DATA step variable Course_code and to assign the
corresponding value of Course_title to each macro variable. That is, for each
observation in Sasuser.Courses, the macro processor will create a new macro variable.
The new macro variable will have the same name as the value of the data set variable
Course_code for that observation. The value of the new macro variable will be the
value of the data set variable Course_title for that observation.

data _null_;
set sasuser.courses;
call symput(course_code, trim(course_title));
run;
%put _user_;       用一个 call symput 建立了好多个macro variable 在data step里。

12.


13. options symbolgen;
data _null_;
set sasuser.schedule;
call symput(’teach’||left(course_number),
trim(teacher));
run;


14.
this happens at Data step execution time.


15.
This form of the INTO clause does not trim leading or trailing blanks. Also, the
INTO clause cannot be used when you create a table or a view.

%let totalfee=&totalfee;
Note: This form of the INTO clause does not trim leading or trailing blanks, but the
%LET statement removes any leading or trailing blanks that are stored in the value of
totalfee.

16.  create multiple macro variables in PROC SQL;
proc sql;
select course_code, location, begin_date format=mmddyy10.
into :crsid1-:crsid3,
:place1-:place3,
:date1-:date3
from sasuser.schedule
where year(begin_date)=2002
order by begin_date;
quit;

17. 当不知道需要建立多少个macro  variable 的时候,先建立一个macro variable是一共多少行,然后和别的需要的连接在一起,例子如下:


18. 
这个是只想建立一个 macro variable但是要存储很多个值的情况。

19.    proc sql noprint;
select distinct location into :sites separated by ’ ’
from sasuser.schedule;
quit;

20. 用macro view 在 sql view里。

21. 


QUIZ:
1. d----------c          macro functions are handled before any SAS statement, not macro variable.
2. a              in symput, the macro variable name and value name, both need ' ' 
3. d
4. c-----b     If you enclose the DATA step
variable name in quotation marks in the SYMPUT routine, the new macro
variable will have the same name as the DATA step variable rather than having
the DATA step variable’s value as a name.

5. b
6. d
7. c
8. c
9. b----d      price=symget("daily_fee");         needs " "
10. c        in proc sql,    needs input()   to convert character to numeric.



没有评论: