Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Macro Variables in SQL (Review)

Similar presentations


Presentation on theme: "Creating Macro Variables in SQL (Review)"— Presentation transcript:

1 Creating Macro Variables in SQL (Review)
4 Creating Macro Variables in SQL (Review)

2 The SQL Procedure INTO Clause
The INTO clause creates macro variables. General form of the SQL procedure INTO clause: This form of the INTO clause does not trim leading or trailing blanks. SELECT col1, col2, INTO :mvar1, :mvar2,... FROM table-expression WHERE where-expression other clauses;

3 The SQL Procedure INTO Clause
Example: Create a macro variable that contains the total price of all Internet orders. proc sql noprint; select sum(total_retail_price) format=dollar8. into : total from orion.order_fact where year(order_date)=2007 and order_type=3; quit; %put Total sales: &total;

4 The INTO clause can create multiple macro variables.
Example: Create macro variables with the date and amount of the top three sales from 2007. title 'Top 2007 Sales'; proc sql outobs=3 double; select total_retail_price, order_date format=mmddyy10. into :price1-:price3, :date1-:date3 from orion.order_fact where year(order_date)=2007 order by total_retail_price desc; quit; title; %put Top 3 sales amounts: #1: &price1 #2: &price2 #3: &price3; %put Top 3 sales dates: #1: &date1 #2: &date2 #3: &date3; In addition to the shaded items, not the ORDER BY clause with the DESC option.

5 The SQL Procedure INTO Clause
The INTO clause can store the unique values of a specified column in a single macro variable. SELECT DISTINCT col1, . . . INTO :mvar SEPARATED BY 'delimiter', . . . FROM table-expression WHERE where-expression other clauses;

6 The SQL Procedure INTO Clause
Example: Create a macro variable with a list of all customer countries. Delimit the country codes with a comma and space. proc sql noprint; select distinct country into :countries separated by ', ' from orion.customer; quit; %put Customer Countries: &Countries;

7 The SQL Procedure Example: Display all user-defined macro variables.
proc sql; select name, value from dictionary.macros where scope='GLOBAL' order by name; quit; Not as easy/concise as %PUT _user_; but the ORDER BY clause insures the list of macro variables is alphabetical. One could very easily create a macro called PUTUSER or whatever.

8 The SQL Procedure INTO Clause
Example: Create a macro variable with a list of all user-defined macro variable names. Delimit the names with spaces. proc sql noprint; select name into: vars separated by ' ' from dictionary.macros where scope='GLOBAL'; quit; %put &vars; This is not much different than the example on slide 87, but this is leading up to something. Stay tuned.

9 Example: Create a utility macro that deletes all user-defined macro variables.
%macro deletemymacvars; proc sql noprint; select name into: vars separated by ' ' from dictionary.macros where scope='GLOBAL'; quit; %symdel &vars; %mend deletemymacvars; As promised way back in chapter 2, a way to delete all user-defined macro variables. This may produce an error, but it works regardless. The error is caused by an automatic SQL macro variable that can't be deleted because it is scoped incorrectly. This has been reported to tech support and will be fixed.

10 proc sql; select name from dictionary.macros where scope="GLOBAL"; quit; %deletemymacvars


Download ppt "Creating Macro Variables in SQL (Review)"

Similar presentations


Ads by Google