macro variables

Transcription

macro variables
WEBINAR@LUNCHTIME
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
THEMA: „ZAUBERN MIT SAS MACROS –
LOOKUP-TABELLEN LEICHT GEMACHT"
DR. DOROTHEE HILDEBRANDT
WEBINAR@LUNCHTIME HERZLICH WILLKOMMEN BEI WEBINAR@LUNCHTIME
Moderation
Training
Anne K. Bogner-Hamleh
Dr. Dorothee Hildebrandt
SAS Institute GmbH
Education Consultant
SAS Institute GmbH
Senior Technical Trainer
Xing-Profil:
http://www.xing.com/profile/AnneKatrin_BognerHamleh?key=0.0
Hinweise zum Ablauf des Webinars:
• Teilnehmer sind automatisch “stumm” geschaltet
• Sie können Nachrichten an den Moderator senden und Fragen stellen
• die Veranstaltung wird aufgezeichnet
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME AGENDA
• Introduction
• Macro Variables
• Formats
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME INTRODUCTION
•
Define table lookup techniques.
• Describe the chapter business scenario.
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME BUSINESS SCENARIO
•
Automate the creation and use of lookup tables to translate data values into
descriptive labels.
Continent_
ID
91
93
94
95
96
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
Continent_
Name
North America
Europe
Africa
Asia
Australia/Pacific
WEBINAR@LUNCHTIME BUSINESS SCENARIO
Lookup tables reside in tables, in SAS data sets.
Continent_
ID
orion.continent
91
93
94
95
96
Country
orion.country
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
AU
CA
DE
IL
TR
US
ZA
Continent_
Name
North America
Europe
Africa
Asia
Australia/Pacific
Country_Name
Australia
Canada
Germany
Israel
Turkey
United States
South Africa
WEBINAR@LUNCHTIME SOLUTIONS
Solution 1: Load a lookup table into macro variables.
Perform the lookup with
– an indirect reference
– the SYMGET function.
orion.continent
Solution 2: Load a lookup table into a format.
Perform the lookup with a macro function.
orion.continent
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME MACRO VARIABLES - OBJECTIVES
•
•
•
•
Load a lookup table into a series of macro variables.
Perform lookups with indirect references.
Perform lookups with the SYMGET function.
Use numeric and character lookup keys.
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME SOLUTION 1
Step 1
Load a lookup table into macro variables.
Step 2a
Perform the lookup with an indirect reference.
Step 2b
Perform the lookup with the SYMGET function
orion.continent
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME NUMERIC LOOKUP KEY
How can you automate the lookup?
proc print data=orion.customer;
var Customer_ID Gender Customer_Name
Country Continent_ID;
where Continent_ID=93;
numeric value
title "Customers from Europe";
run;
title;
text label
Hint:
• The Continent_ID and Continent_Name variables are stored in
orion.continent.
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME NUMERIC LOOKUP KEY
How can you load this lookup table into a series of macro variables?
ORION.CONTINENT
Obs
Continent_
ID
1
2
3
4
5
91
93
94
95
96
numeric key
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
Continent_Name
North America
Europe
Africa
Asia
Australia/Pacific
WEBINAR@LUNCHTIME QUIZ
•
To look up a continent name from a continent ID, how many leading
ampersands are required?
a.1
b.2
c. 3
d.4
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
Variable
continent
C91
C93
C94
C95
C96
Value
93
North America
Europe
Africa
Asia
Australia/Pacific
WEBINAR@LUNCHTIME QUIZ - CORRECT ANSWER
•
To look up a continent name from a continent ID, how many leading
ampersands are required?
a.1
b.2
c. 3
d.4
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
Variable
continent
C91
C93
C94
C95
C96
Value
93
North America
Europe
Africa
Asia
Australia/Pacific
WEBINAR@LUNCHTIME NUMERIC LOOKUP KEY
Step 2a
Perform the lookup with an indirect reference.
%let continent=93;
proc print data=orion.customer;
var Customer_ID Gender Customer_Name Country
Continent_ID;
where Continent_ID=&continent;
title "Customers from &&C&continent";
run;
title;
indirect reference
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME NUMERIC LOOKUP KEY
An indirect reference causes a second scan.
reference
first scan
second scan
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
&&C&continent
&C93
Europe
WEBINAR@LUNCHTIME SYMGET FUNCTION
Step 2b
Perform the lookup with the SYMGET function
data customers;
set orion.customer;
length Continent $ 17;
Continent=symget('C' || left(Continent_ID));
run;
SYMGET(macro-variable)
macro-variable can be a
•
character literal
• character variable
• character expression.
•
The SYMGET function returns
the value of a macro variable.
Variables created by the SYMGET function are character with a length of 200
unless previously defined.
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME CHARACTER LOOKUP KEY
proc print data=orion.customer;
var Customer_ID Gender Customer_Name Country
Continent_ID;
where Country="DE";
title "Customers from Germany"; character value
run;
title;
text label
How can you automate the lookup?
Hint:
• The Country and Country_Name variables are stored in orion.country.
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME CHARACTER LOOKUP KEY
ORION.COUNTRY
Obs
Country
Country_Name
1
2
3
4
5
6
7
AU
CA
DE
IL
TR
US
ZA
Australia
Canada
Germany
Israel
Turkey
United States
South Africa
character key
How can you load this lookup table into a series of macro variables?
Hint: Country values represent valid SAS names.
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME QUIZ
•
To look up a country name from a country ID, how many leading ampersands
are required?
a.1
b.2
c. 3
d.4
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
Variable
country
CA
IL
TR
AU
US
DE
ZA
Value
DE
Canada
Israel
Turkey
Australia
United States
Germany
South Africa
WEBINAR@LUNCHTIME QUIZ
•
To look up a country name from a country ID, how many leading ampersands
are required?
a.1
b.2
c. 3
d.4
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
Variable
country
CA
IL
TR
AU
US
DE
ZA
Value
DE
Canada
Israel
Turkey
Australia
United States
Germany
South Africa
WEBINAR@LUNCHTIME CHARACTER LOOKUP KEY
Step 2a
Perform the lookup with an indirect reference.
%let country=DE;
proc print data=orion.customer;
var Customer_ID Gender Customer_Name Country
Continent_ID;
where Country="&country";
title "Customers from &&&country";
run;
title;
Use three ampersands when the value
of one macro variable represents the
exact name of another macro variable.
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME CHARACTER LOOKUP KEY
The indirect reference causes a second scan.
reference
first scan
second scan
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
&&&country
&DE
Germany
WEBINAR@LUNCHTIME CHARACTER LOOKUP KEY
Step 2b
Perform the lookup with the SYMGET function
data customers;
set orion.customer;
length Country_Name $ 13;
Country_Name=symget(country);
run;
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME FORMATS - OBJECTIVES
•
Load a lookup table into a user-defined format.
• Use the PUTN and PUTC functions to perform the lookup.
• Use numeric and character lookup keys.
• Apply a SAS format.
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME SOLUTION 2
Step 1 Load a lookup table into a user-defined format
Step 2
Perform the lookup with a macro function.
orion.continent
%Makefmt
orion.country
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME USER-DEFINED FORMATS
Step 1
Load a lookup table into a user-defined format.
data fmtdata;
keep start label fmtname;
retain fmtname "continent";
set orion.continent(rename=(
Continent_ID=start
Continent_Name=label));
run;
required variables
proc format cntlin=fmtdata fmtlib;
select Continent;
title "CONTINENT format based on ORION.CONTINENT";
run;
title;
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME USER-DEFINED FORMATS
•
The Makefmt macro creates a format from a data set.
%macro makefmt(fmtname,dsn,start,label);
data fmtdata;
keep start label fmtname;
retain fmtname "&fmtname";
set &dsn(rename=(
&start=start
&label=label));
run;
proc format cntlin=fmtdata fmtlib;
select &fmtname;
title "%upcase(&fmtname) format based on %upcase(&dsn)";
run;
title;
%mend makefmt;
%makefmt(continent,orion.continent,continent_ID,continent_name)
%makefmt($country,orion.country,country,country_name)
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME SOLUTION 2
Complete
Step 1
Load a lookup table into a user-defined format
Next task
Step 2
Perform the lookup with a macro function.
orion.continent
%Makefmt
orion.country
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME BUSINESS SCENARIO
•
Create %FMTC and %FMTN macro functions to apply a user-defined or SAS
format to a macro variable.
%let country=AU;
%FMTC(&country,$country.)
%let continent=96;
Australia
%FMTN(&continent,continent.)
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME CHARACTER LOOKUP KEY
Step 2
Perform the lookup with a macro function.
%macro fmtc(value,format);
%sysfunc(putc(&value,&format))
%mend fmtc;
Create once.
Use forever.
%let country=DE;
proc print data=orion.customer;
var Customer_ID Gender Customer_Name Country
Continent_ID;
where Country="&country";
title "Customers from %fmtc(&country,$country.)";
run;
title;
%fmtc(DE,$country.)
Germany
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME NUMERIC LOOKUP KEY
Step 2
Perform the lookup with macro function.
%macro fmtn(value,format);
Create once.
%sysfunc(putn(&value,&format)) Use forever.
%mend fmtn;
%let continent=93;
proc print data=orion.customer;
var Customer_ID Gender Customer_Name Country
Continent_ID;
where Continent_ID=&continent;
title "Customers from %fmtn(&continent,continent.)";
run;
title;
%fmtn(93,continent.)
Europe
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME BUSINESS SCENARIO
Apply a SAS format to title text for improved readability.
Orders over €1.000
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
Apply the Euro format.
Obs
Order_ID
Order_
Date
22
41
54
72
120
206
262
1230591684
1230793366
1231014780
1231227910
1231976710
1233689304
1235744141
18APR2007
25MAY2007
04JUL2007
13AUG2007
26DEC2007
10SEP2008
10MAY2009
Quantity
4
3
4
3
2
3
3
Total_Retail_
Price
$1,796.00
$1,250.40
$1,064.00
$1,266.00
$1,200.20
$1,514.40
$1,687.50
WEBINAR@LUNCHTIME SAS FORMATS
Apply a SAS numeric format.
%macro fmtn(value,format);
%sysfunc(putn(&value,&format))
%mend fmtn;
%let total=1000;
proc print data=orion.order_fact;
var Order_ID Order_Date Quantity
Total_Retail_Price;
where Total_Retail_Price>&total;
title "Orders over %fmtn(&total,eurox11.)";
run;
title;
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME SAS FORMATS
%macro fmtn(value,format);
%left(%sysfunc(putn(&value,&format)))
%mend fmtn;
%let total=1000;
proc print data=orion.order_fact;
var Order_ID Order_Date Quantity
Total_Retail_Price;
where Total_Retail_Price>&total;
title "Orders over %fmtn(&total,eurox11.)";
run;
title;
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME COMPARISON OF TABLE LOOKUP TECHNIQUES
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
Macro variables
Formats
Easy to create
Might already exist
Memory storage
Disk storage
Reference anywhere
Reference anywhere a format is
accepted within a DATA or PROC step
Character lookup key
values must represent valid
macro variable names
Indirect reference not needed
FRAGEN?
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME VIELEN DANK FÜR IHRE TEILNAHME
•
Interesse an weiterem Austausch?
•
Diskutieren Sie mit uns in der XING-Gruppe
Business Analytics mit SAS
•
Sprechen Sie uns direkt an:
[email protected]
•
Treffen Sie uns auf Veranstaltungen:
18. KSFE in Göttingen, 27.-28.3.2014
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
WEBINAR@LUNCHTIME WEITERE INFORMATIONEN UND KURSE ZU DIESEM THEMA…
• SAS® Makrosprache 2: Praxis für Fortgeschrittene
25.03. - 26.03.14 Köln
23.04. - 24.04.14 Hamburg
26.05. - 27.05.14 Wien
02.06. - 03.06.14 Frankfurt
• SAS® Makrosprache 1: Grundlagen
31.03. - 02.04.14 München
28.04. - 30.04.14 Hamburg
05.05. - 07.05.14 Wien
26.05. - 28.05.14 Heidelberg
10.06. - 12.06.14 Zürich
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
Wussten Sie schon, was
Sie tun können, wenn
sich die Werte, die ein
selbstgeschriebenes
SAS Format anzeigen
sollen, häufig ändern?
WEBINAR@LUNCHTIME NÄCHSTES WEBINAR@LUNCHTIME
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
FOLIEN ZUM DOWNLOAD UNTER
WWW.SAS.DE/LUNCHTIME
WIE HAT IHNEN UNSER WEBINAR GEFALLEN?
C op yr i g h t © 2 0 1 4 , S A S I n s t i t u t e I n c . A l l r i g h t s r es er v e d .
www.SAS.com

Similar documents