Thursday, December 2, 2010

A SAS example about %str


The question is:
1: Copy the program shown below and paste it into the code editing window. Change the name pattern in the WHERE statement and TITLE2 statement to O'Savio. Modify the quotation marks as needed.
proc print data=sasuser.all noobs label uniform;
   where student_name contains 'Babbit';
   by student_name student_company;
   var course_title begin_date location teacher;
   title 'Courses Taken by Selected Students:';
   title2 'Those with Babbit in Their Name';
run;
Submit the modified program.
2: Modify the program so that the two occurrences of O'Savio are replaced by references to a macro variable named pattern. Submit the program and examine the output.


Answer for 1:
proc print data=sasuser.all noobs label uniform;
   where student_name contains "O'Savio"; *use double quotation since there is single quotation in O'Savio;
   by student_name student_company;
   var course_title begin_date location teacher;
   title 'Courses Taken by Selected Students:';
   title2 "Those with O'Savio in Their Name";
run;

Answer for 2:
%let pattern=%str(O%'Savio); /* The third % is for ' */
proc print data=sasuser.all noobs label uniform;
   where student_name contains "&pattern"; /* double quotation since O'savio has ' */
   by student_name student_company;
   var course_title begin_date location teacher;
   title 'Courses Taken by Selected Students:';
   title2 “Those with &pattern in Their Name”;
run;

No comments:

Post a Comment