All of the data necessary for this assignment are found in the mac1 subdirectory

1. Displaying Automatic Macro Variables

Use the %PUT statement to list all automatic macro variables in the SAS log.

2. Using Automatic Macro Variables

In [ ]:
 

a. Using the SORT procedure, sort the data set orion.continent by Continent_Name. Use the OUT= option in the PROC SORT statement to create a temporary data set in the work library so that you do not overwrite the original data set.

b. Using the PRINT procedure and an automatic macro variable, print the most recently created data set and display the data set name in the title.

3. Using Automatic Macro Variables

a. Submit the following program. Add a %put statement to the program to display the automatic variable SYSLAST.

In [ ]:
data new;
   set orion.continent;
run;

Now submit the following program after adding a %put statement at the end to display the automatic variable SYSLAST.

In [ ]:
proc print data=orion.continent;
run;

4. Using SAS Date Constants

Use the following starter program

In [ ]:
proc print data=orion.employee_payroll;
   format Birth_Date Employee_Hire_Date date9.;
run;

Modify the program so that it subsets the data to return only the employees hired between January 1, 2007, and today. Use the automatic macro variable SYSDATE9 to return today’s date. Add a title to list the dates of hire included in the report

5. Defining and Using Macro Variables for Character Substitution

Use the following starter program.

In [ ]:
proc print data=orion.customer_dim;
   var Customer_Name Customer_Gender Customer_Age;
   where Customer_Group contains 'Gold';
   title 'Gold Customers';
run 

a. Modify the program so that the two occurrences of Gold are replaced by references to the macro variable TYPE. Precede the program with a %LET statement to assign the value Gold to TYPE. Submit the program. Verify that it produces the same output as before.

b. Include the appropriate system option to display resolved values of macro variables in the SAS log. Resubmit the program and examine the log.

c. Modify the value of TYPE to Internet. Resubmit the program and examine the log.

d. Turn off the system option from part b above

6. Defining and Using Macro Variables for Numeric Substitution

Use the program developed in #5 above as a starter program. Edit the program to display only the Gold level customers between the ages of 30 to 45.

a. Modify the program so that the values 30 and 45 are replaced by references to the macro variables AGE1 and AGE2, respectively.

b. Include the appropriate system option to display resolved values of macro variables in the SAS log. Resubmit the program and examine the log.

c. Modify the values of AGE1 and AGE2, assign the value 25 to AGE1 and the value 40 to AGE2. Resubmit the program and examine the log.

d. Turn off the system option from part b above.

7. Deleting Macro Variables

Use the following starter program.

In [ ]:
%let book1=Asimov;
%let book2=Adams;

a. Add a statement to display the value of the macro variables book1 and book2 in the sas log.

b. Add a statement to delete the user-defined macro variables, book1 and book2.

c. Use a %PUT statement to display all user defined macro variables and verify that the macro variable deletion was successful.

8. Consecutive Macro Variables References

a. Submit the program below and examine the output that it creates.

In [ ]:
proc print data=orion.organization_dim;
   where Employee_Hire_Date='01AUG2006'd;
   id Employee_ID;
   var Employee_Name Employee_Country Employee_Hire_Date;
   title 'Personal Information for Employees Hired in AUG 2006';
run;

b. Modify the program so that the two occurrences of AUG and 2006 are replaced by references to the macro variables MONTH and YEAR, respectively. Precede the program with a %LET statement to assign the value AUG to MONTH and the value 2006 to YEAR. Submit the program and verify that it produces the same output as before.

c. Modify the value of MONTH to JUL and YEAR to 2003. Resubmit the program.

9. Macro Variable References with Delimiters

a. Submit the program below and examine the output that it creates.

In [ ]:
proc print data=orion.organization_dim;
   id Employee_ID;
   var Employee_Name Employee_Country Employee_Gender;
   title 'Listing of All Employees From Orion.Organization_Dim';
run;

b. Modify the program so that all occurrences of Organization and Employee are replaced with macro variable references called DSN and VAR, respectively. Submit the program and verify the output.

When substituting for the hardcoded Employees in the TITLE statement, be sure to keep the ending s as part of the title text.

c. Modify the value of DSN to Customer and VAR to Customer. Resubmit the program.

10. Macro Variable References with Multiple Delimiters

a. The program below analyzes the orion.staff data to find the employee with the most seniority within a job title. Submit the program and examine the output that it creates.

In [ ]:
proc sort data=orion.staff out=staffhires;
   by Job_Title Emp_Hire_Date;
run;
data FirstHired;   
   set staffhires;
   by Job_Title;
   if First.Job_Title;  
run;
proc print data=FirstHired;
   id Job_Title;
   var Employee_ID Emp_Hire_Date;
   title "First Employee Hired within Each Job Title";  
run;

b. Using a macro variable, modify the program to return the employees with the most or least amount of seniority. Be sure to make any necessary modifications to the title.

11. Using the %SUBSTR and %SCAN Functions

a. Submit a %LET statement to assign the value Anthony Miller to a macro variable named FULLNAME.

b. Extract the first initial and last name, putting them together into a new macro variable as A. Miller.

Use the %PUT statement to display the results.

12. Using the %SYSFUNC Function

Use the %PUT statement and the %SYSFUNC function to display the current date and time. Format the date with the MMDDYYP10.format and the time with TIMEAMPM. format.

13. Protecting Special Characters

a. Submit the following program and examine the output that it creates.

In [ ]:
proc print data=orion.product_dim;
   where Product_Name contains "Jacket";
   var Product_Name Product_ID Supplier_Name;
   title "Product Names Containing 'Jacket'";
run;

b. Submit a %LET statement to assign the value R&D to a macro variable named PRODUCT. Use the new macro variable in the WHERE statement and the TITLE statement. Submit the modified program.

c. Add the current date and time to a title. Format the date with the MMDDYYP10. format and the time with the TIMEAMPM. format. Remove any extra blanks in the title.

14. Verifying a Data Set Name

SAS includes macros that are referred to as "autocall." One such macro is VERIFY (Essentially, autocall macros are macros that can be called without including them in a program. We will cover the autocall facility and developing autocall libraries in a later topic.)

The general call of the VERIFY macro is:

%VERIFY(source,excerpt)

%VERIFY returns the position of the first character in source that is not also present in excerpt. If all characters in source are present in excerpt, %VERIFY returns 0.

In [1]:
/*example*/
%let try1=%VERIFY(ABC,abcdefg);
%put try1:  &try1;/* 1*/

%let try2=%VERIFY(def,abcdefg);
%put try2:  &try2;/* 0*/
Out[1]:

11   ods listing close;ods html5 file=stdout options(bitmap_mode='inline') device=png; ods graphics on / outputfmt=png;
NOTE: Writing HTML5 Body file: STDOUT
12
13 /*example*/
14 %let try1=%VERIFY(ABC,abcdefg);
15 %put try1: &try1;/* 1*/
try1: 1
16
17 %let try2=%VERIFY(def,abcdefg);
18 %put try2: &try2;/* 0*/
try2: 0
19
20 ods html5 close;ods listing;

21

a. Create a macro variable named DSN with a value of work.test

b. Create a macro variable named FIRST that contains the first character of the dataset name (in this case t). Use a %put statement to verify that you have defined the macro variable FIRST correctly.

c. Create a macro variable named VALID that contains the valid first characters for a dataset. (Recall a data set may start with an underscore or alphabetic character, case insensitive.)

d. Use the %verify macro in the definition of a macro variable CKFIRST. CKFIRST should be 0 if FIRST is a correct character, 1 otherwise. Use a %put statement to display CKFIRST and verify that you obtained the correct value (0).

e. Rerun your program changing the value of DSN to be work.1test. Use a %put to display the value of CKFIRST and verify that you obtained the correct value (1).