Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Webinar@LunchtimeEffizientes Programmieren mit Suchtfaktor: SAS Hash-Tables
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Table Lookup Technik: Hash TablesWerte aus einer Referenztabelle abfragen mittels Hash Tables
DATA Step Hash Objekte: Eigenschaften
Datenspeicherung und Suche im Arbeitsspeicher
Hash Objekt muss in den Arbeitsspeicher passen (kleinere Datei nehmen)
Schnelle Alternative zu Data Step Merge oder SQL Joins
Anders als bei Arrays können verschiedene Datentypen zusammen in einem Objekt sein
Daten müssen nicht sortiert sein
Nutzt den Schlüssel für eine schnelle Datensuche
Dot-Net Syntax (objektorientiert)
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Szenario
Teilauszug aus der Datei: orion.supplier
Supplier_
IDSupplier_Name
Street_
IDSupplier_Address
Sup_
Street_N
umber
Country
50Scandinavian
Clothing A/S6850100389 Kr. Augusts Gate 13 13 NO
109 Petterson AB 8500100286 Blasieholmstorg 1 1 SE
316 Prime Sports Ltd 9250103252 9 Carlisle Place 9 GB
755 Top Sports 3150108266 Jernbanegade 45 45 DK
772AllSeasons Outdoor
Clothing9260115819 553 Cliffview Dr 553 US
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Die Datei orion.supplier beinhaltet demographische Informationenüber Lieferanten (keine Produktinfos).
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Szenario
Teilauszug aus der Datei: orion.product_list
Product_ID Product_NameSupplier_
ID
Product_
Level
Product_
Ref_ID
210000000000 Children . 4 .
210100000000 Children Outdoors . 3 210000000000
210100100000 Outdoor things, Kids . 2 210100000000
210200000000 Children Sports . 3 210000000000
210200100000 A-Team, Kids . 2 210200000000
210200100009Kids Sweat Round
Neck,Large Logo3298 1 210200100000
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Es müssen Daten aus der Datei orion.supplier mit orion.product_listverbunden werden, welche Produktinformationen beinhaltet.
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Ein Hash Objekt aus einer SAS Datei befüllendata supplier_info;
length Supplier_Name $40 Supplier_Address $ 45
Country $ 2;
if _N_=1 then do;
declare hash S(dataset:'orion.supplier');
S.definekey('Supplier_ID');
S.definedata('Supplier_Name',
'Supplier_Address','Country');
S.definedone();
call missing(Supplier_Name,
Supplier_Address,Country);
end;
set orion.product_list;
rc=S.find();
if rc=0;
drop rc;
run;
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Hash Objekt erstellen
Erstellen eines Hash Objekts namens T.
declare hash T();
declare hash T(dataset: 'orion.MemberType');
Erstellen des T Hash Objekts und Laden aus der Datei
orion.MemberType.
Copyright © SAS Inst itute Inc. A l l r ights reserved.
data supplier_info;drop rc;length Supplier_Name $40
Supplier_Address $ 45 Country $ 2;
if _N_=1 then do;declare hash S(dataset:'orion.supplier');S.definekey('Supplier_ID');S.definedata('Supplier_Name',
'Supplier_Address','Country');
S.definedone();call missing(Supplier_Name,
Supplier_Address,Country);
end;set orion.product_list;rc=S.find();if rc=0;
run;
Ausführung
Partial PDVSupplier_
Name
Supplier_
AddressCountry
Product
_ID
Product_
Name
Supplier
_ID
. .
rc _N_
. 1. . .
D
Auszug: HASH Object S
KEY:
Supplier
_ID
DATA:
Supplier_
Name
DATA:
Supplier_
Address
DATA:
Country
50Scandinavian
Clothing A/S
Kr.
Augusts
Gate 13
NO
109 Petterson ABBlasieh-
olmstorg 1SE
316 Prime Sports Ltd9 Carlisle
PlaceGB
.
.
.
.
.
.
.
.
.
.
.
.
3298 A Team Sports2687 Julie
Ann CtUS
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Partial PDVSupplier_
Name
Supplier_
AddressCountry
Product
_ID
Product_
Name
Supplier
_ID
210000000000 Children .
rc _N_
-2147450842 1. . .
D
Auszug: HASH Table S
KEY:
Supplier
_ID
DATA:
Supplier_
Name
DATA:
Supplier_
Address
DATA:
Country
50Scandinavian
Clothing A/S
Kr.
Augusts
Gate 13
NO
109 Petterson ABBlasieh-
olmstorg 1SE
316 Prime Sports Ltd9 Carlisle
PlaceGB
.
.
.
.
.
.
.
.
.
.
.
.
3298 A Team Sports2687 Julie
Ann CtUS
data supplier_info;drop rc;length Supplier_Name $40
Supplier_Address $ 45 Country $ 2;
if _N_=1 then do;declare hash S(dataset:'orion.supplier');S.definekey('Supplier_ID');S.definedata('Supplier_Name',
'Supplier_Address','Country');
S.definedone();call missing(Supplier_Name,
Supplier_Address,Country);
end;set orion.product_list;rc=S.find();if rc=0;
run;
-2147450842
Ausführung
Copyright © SAS Inst itute Inc. A l l r ights reserved.
data supplier_info;drop rc;length Supplier_Name $40
Supplier_Address $ 45 Country $ 2;
if _N_=1 then do;declare hash S(dataset:'orion.supplier');S.definekey('Supplier_ID');S.definedata('Supplier_Name',
'Supplier_Address','Country');
S.definedone();call missing(Supplier_Name,
Supplier_Address,Country);
end;set orion.product_list;rc=S.find();if rc=0;
run;
Ausführung
Partial PDVSupplier_
Name
Supplier_
AddressCountry
Product
_ID
Product_
Name
Supplier
_ID
210000000000 Children .
rc _N_
-2147450842 1
. . . D
Auszug: HASH Object S
KEY:
Supplier
_ID
DATA:
Supplier_
Name
DATA:
Supplier_
Address
DATA:
Country
50Scandinavian
Clothing A/S
Kr.
Augusts
Gate 13
NO
109 Petterson ABBlasieh-
olmstorg 1SE
316 Prime Sports Ltd9 Carlisle
PlaceGB
.
.
.
.
.
.
.
.
.
.
.
.
3298 A Team Sports2687 Julie
Ann CtUS
False
-2147450842
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Partial PDVSupplier_
Name
Supplier_
AddressCountry
Product
_ID
Product_
Name
Supplier
_ID
210200100009Kids Sweat Round
Neck,Large Logo 3298
rc _N_
. 6
. . . D
Auszug: HASH Object S
KEY:
Supplier
_ID
DATA:
Supplier_
Name
DATA:
Supplier_
Address
DATA:
Country
50Scandinavian
Clothing A/S
Kr.
Augusts
Gate 13
NO
109 Petterson ABBlasieh-
olmstorg 1SE
316 Prime Sports Ltd9 Carlisle
PlaceGB
.
.
.
.
.
.
.
.
.
.
.
.
3298 A Team Sports2687 Julie
Ann CtUS
data supplier_info;drop rc;length Supplier_Name $40
Supplier_Address $ 45 Country $ 2;
if _N_=1 then do;declare hash S(dataset:'orion.supplier');S.definekey('Supplier_ID');S.definedata('Supplier_Name',
'Supplier_Address','Country');
S.definedone();call missing(Supplier_Name,
Supplier_Address,Country);
end;set orion.product_list;rc=S.find();if rc=0;
run;
Continue until
_N_=6
Ausführung
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Partial PDVSupplier_
Name
Supplier_
AddressCountry
Product
_ID
Product_
Name
Supplier
_ID
210200100009Kids Sweat Round
Neck,Large Logo 3298
rc _N_
0 6
. . .D
Auszug: HASH Object S
KEY:
Supplier
_ID
DATA:
Supplier_
Name
DATA:
Supplier_
Address
DATA:
Country
50Scandinavian
Clothing A/S
Kr.
Augusts
Gate 13
NO
109 Petterson ABBlasieh-
olmstorg 1SE
316 Prime Sports Ltd9 Carlisle
PlaceGB
.
.
.
.
.
.
.
.
.
.
.
.
3298 A Team Sports2687 Julie
Ann CtUS
data supplier_info;drop rc;length Supplier_Name $40
Supplier_Address $ 45 Country $ 2;
if _N_=1 then do;declare hash S(dataset:'orion.supplier');S.definekey('Supplier_ID');S.definedata('Supplier_Name',
'Supplier_Address','Country');
S.definedone();call missing(Supplier_Name,
Supplier_Address,Country);
end;set orion.product_list;rc=S.find();if rc=0;
run;
Ausführung
Copyright © SAS Inst itute Inc. A l l r ights reserved.
data supplier_info;drop rc;length Supplier_Name $40
Supplier_Address $ 45 Country $ 2;
if _N_=1 then do;declare hash S(dataset:'orion.supplier');S.definekey('Supplier_ID');S.definedata('Supplier_Name',
'Supplier_Address','Country');
S.definedone();call missing(Supplier_Name,
Supplier_Address,Country);
end;set orion.product_list;rc=S.find();if rc=0;
run;
Ausführung
Partial PDVSupplier_
Name
Supplier_
AddressCountry
Product
_ID
Product_
Name
Supplier
_ID
A Team Sports 2687 Julie Ann Ct US 210200100009Kids Sweat Round
Neck,Large Logo 3298
rc _N_
0 6
. . . D
Auszug: HASH Object S
KEY:
Supplier
_ID
DATA:
Supplier_
Name
DATA:
Supplier_
Address
DATA:
Country
50Scandinavian
Clothing A/S
Kr.
Augusts
Gate 13
NO
109 Petterson ABBlasieh-
olmstorg 1SE
316 Prime Sports Ltd9 Carlisle
PlaceGB
.
.
.
.
.
.
.
.
.
.
.
.
3298 A Team Sports2687 Julie
Ann CtUS
TrueImplicit OUTPUT;
Implicit RETURN;
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Die Find-Methode
Die FIND Methode lokalisiert den Schlüsselwert im Hash Objekt und gibt die Datenwerte zurück.
Allgemeine Form:
Der ReturnCode kann mit bedingter Logik verbunden werden, um verschiedene ReturnWerte abzufragen.
Beispiel: Werte der ReturnCode VariablenNull (0) Übereinstimmung
Nicht null Keine Übereinstimmung
rc=object.FIND(<KEY: keyvalue-1,..., KEY: keyvalue-n>);
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Length Anweisung
data supplier_info;drop rc;
length Supplier_Name $40 Supplier_Address $ 45 Country $ 2;
if _N_=1 then do;declare hash S(dataset:'orion.supplier');S.definekey('Supplier_ID');S.definedata('Supplier_Name',
'Supplier_Address','Country');
S.definedone();.....
end;
S.definedata:Die Datenkomponenten beziehen sich auf SAS Variablen und sollen als SAS Variablen auch angelegt werden , um automatisch befüllt werden zu können.
Wie?Eine Möglichkeit: LENGTH nutzen Damit legt SAS in der Kompilierungsphase im Data Step diese als SAS Variablen im Zwischenspeicher PDV an.Partial PDV
Supplier_
Name
Supplier_
Address
Count
ry
Product
_ID
Product_
Name
Supplie
r
_ID
. .
Copyright © SAS Inst itute Inc. A l l r ights reserved.
CALL MISSING
data supplier_info;drop rc;length Supplier_Name $40
Supplier_Address $ 45
Country $ 2;
if _N_=1 then do;...S.definedata('Supplier_Name',
'Supplier_Address','Country');
...
call missing(Supplier_Name,Supplier_Address,Country);
Da die Datenelemente als SAS Variablen erstellt werden sollen -mittels Length Anweisung - haben sie jedoch keinen Startwert (Initialisierungswert).
Sie kommen über das Hash Objekt, direkt in den PDV, nicht aus der Datei, und sie werden auch nicht zugewiesen mit Gleichzeichen =
Dies erzeugt folgende Meldung im Log:
NOTE: Variable Supplier_Name is uninitialized.
NOTE: Variable Supplier_Address is uninitialized.
NOTE: Variable Country is uninitialized.
Call Missing:- Verhindert Note im Log- Weist Missingwerte als
Startwert zu
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Ergebnisse
Product Information
Obs Product_ID Supplier_ID Supplier_Name Supplier_Address Country
1 210200100009 3298 A Team Sports 2687 Julie Ann Ct US
2 210200100017 3298 A Team Sports 2687 Julie Ann Ct US
3 210200200022 6153 Nautlius SportsWear Inc 56 Bagwell Ave US
4 210200200023 6153 Nautlius SportsWear Inc 56 Bagwell Ave US
5 210200300006 1303 Eclipse Inc 1218 Carriole Ct US
6 210200300007 1303 Eclipse Inc 1218 Carriole Ct US
7 210200300052 1303 Eclipse Inc 1218 Carriole Ct US
8 210200400020 1303 Eclipse Inc 1218 Carriole Ct US
9 210200400070 1303 Eclipse Inc 1218 Carriole Ct US
10 210200500002 772 AllSeasons Outdoor Clothing 553 Cliffview Dr US
Partial PROC PRINT Output
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Ein weiteres Anwendungsbeispiel für Hash Objekte
• Die Marketing Abteilung will jeweils die beiden Kunden mit demhöchsten und die beiden mit dem niedrigsten Umsatz herausfinden
• Ausgabe in 2 Dateien in EINEM Data Step
$$$ $
Partial orion.orderfact
Customer
IDProductID Quantity
Total
Retail
Price
CostPrice
PerUnit
Discoun
t
63 220101300017 1 $16.50 $7.45 .
5 230100500026 1 $247.50 $109.55 .
45 240600100080 1 $28.30 $8.55 .
41 240600100010 2 $32.00 $6.50 .
183 240200200039 3 $63.60 $8.80 .
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Hash Iterator Objekt (Hiter Objekt)Das Hash Iterator Objekt ist sehr gut geeignet zum effizientenDurchsuchen eines Hash Objekts. Dabei wird der Schlüssel genutzt, die Datei selbst muss nicht sortiert sein, das Hash Iterator Objekt sollte (aber muss nicht) sortiert sein.
Auszug: Hash Object
KEY:TotalRetailPrice
KEY:Customer
ID
16.50 63247.50 5
28.30 4532.00 41
.
.
.
.
.
.95.10 1048.20 1075.20 8933.80 5
Auszug: Hiter View
KEY:TotalRetailPrice
KEY:Customer
ID
1937.20 701001796.00 791687.50 161561.80 183...
.
.
.3.20 693.00 52.70 111712.60 79
Das Hash Iterator Objekt bezieht sich auf ein zuvor erstelltes Hash Objekt
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Anwendungsbeispiel: Syntaxdata top bottom;
drop i;if _N_=1 then do;
if 0 then set orion.orderfact(keep=CustomerID ProductIDTotalRetailPrice);
declare hash customer(dataset:'orion.orderfact',ordered:'descending');
customer.definekey('TotalRetailPrice', 'CustomerID');customer.definedata('TotalRetailPrice', 'CustomerID',
'ProductID');customer.definedone();declare hiter C('customer');
end;C.first();do i=1 to 2;
output top;C.next();
end;C.last();do i=1 to 2;
output bottom;C.prev();
end;stop;
run;
Das Hiter Objekt bezieht sich auf ein zuvor erstelltes Hash
Objekt
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Durchsuchen des Hash Objektes mit demHiter Objekt
• Vier Hiter Objekt Methoden geben die Werte basierend auf der Position im Hash Objekt zurück.
Hash Object
Key: A Data: B
3 X
1 Y
2 Z
PREVLAST
NEXTFIRST
Hiter Object View
Key: A Data: B
3 X
2 Z
1 Y
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Ausführung
...
Auszug: Hash Object customer
KEY:TotalRetailPrice
KEY:Customer
ID
DATA:TotalRetailPrice
DATA:Customer
ID
DATA:Product
ID
16.50 63 16.50 63 220101300017247.50 5 247.50 5 230100500026
28.30 45 28.30 45 24060010008032.00 41 32.00 41 240600100010
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.95.10 10 95.10 10 24050020001648.20 10 48.20 10 24050020012275.20 89 75.20 89 24070020001833.80 5 33.80 5 220101400130
data top bottom;drop i;if _N_=1 then do;
if 0 then set orion.orderfact(keep=CustomerID ProductID
TotalRetailPrice);declare hash customer
(dataset:'orion.orderfact',ordered:'descending');
customer.definekey('TotalRetailPrice','CustomerID');
customer.definedata('TotalRetailPrice','CustomerID','ProductID');
customer.definedone();declare hiter C('customer');
end;
PDVCustomer
ID
Product
ID
Total
RetailPrice i _N_
. . . . 1
DD
Beachten Sie: Das Hash Objekt selbst ist nicht sortiert!
Copyright © SAS Inst itute Inc. A l l r ights reserved.
PDVCustomer
ID
Product
ID
Total
RetailPrice i _N_
. . . . 1
Ausführung
...
Auszug: Hiter C View
KEY:TotalRetailPrice
KEY:Customer
ID
DATA:TotalRetailPrice
DATA:Customer
ID
DATA:Product
ID
1937.20 70100 1937.20 70100 2402001001731796.00 79 1796.00 79 2402001000761687.50 16 1687.50 16 2301007000091561.80 183 1561.80 183 240300300090
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.3.20 69 3.20 69 2301005000043.00 5 3.00 5 2401001004332.70 11171 2.70 11171 2402001000212.60 79 2.60 79 230100500045
data top bottom;drop i;if _N_=1 then do;
if 0 then set orion.orderfact(keep=CustomerID ProductID
TotalRetailPrice);declare hash customer
(dataset:'orion.orderfact',ordered:'descending');
customer.definekey('TotalRetailPrice','CustomerID');
customer.definedata('TotalRetailPrice','CustomerID','ProductID');
customer.definedone();declare hiter C('customer');
end;
Das Hiter Objekt jedoch ist absteigend sortiert, wie in der Syntax angegeben.
DD
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Dateien erstellen mittels Hash Iterator Objekt Methodendata top bottom;
drop i;if _N_=1 then do;
if 0 then set orion.orderfact(keep=CustomerID ProductID TotalRetailPrice);
declare hash customer(dataset:'orion.orderfact',ordered:'descending');
customer.definekey('TotalRetailPrice', 'CustomerID');customer.definedata('TotalRetailPrice', 'CustomerID',
'ProductID');customer.definedone();
declare hiter C('customer');end;
C.first();do i=1 to 2;
output top;C.next();
end;C.last();do i=1 to 2;
output bottom;C.prev();
end;stop;
run;
Copyright © SAS Inst itute Inc. A l l r ights reserved.
PROC PRINT Ausgabe
24
proc print data=top;title 'Top 2 Big Spenders';
run;
proc print data=bottom;title 'Bottom 2 Frugal Spenders';
run;
Top 2 Big Spenders
TotalRetailObs CustomerID ProductID Price
1 70100 240200100173 $1,937.202 79 240200100076 $1,796.00
Bottom 2 Frugal Spenders
TotalRetailObs CustomerID ProductID Price
1 79 230100500045 $2.602 11171 240200100021 $2.70
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Vielen Dank für Ihre Teilnahme
SAS® Live Web Classes 2017:
Termine und Anmeldung
• https://support.sas.com/edu/locations.html?locationId=LW&ctry=DE
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Weitere Informationen und Kurse zu diesem Thema…
SAS® Programmierung 3: Effiziente Techniken des Datenmanagements
03. – 05. April 2017, Heidelberg
19. – 21. Juni 2017, Heidelberg
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Nächstes Webinar@Lunchtime:
27. April 2017
SAS und das Hadoop-Ökosystem
Bastian Weiß, Kybeidos
sas.com
Copyright © SAS Inst itute Inc. A l l r ights reserved.
Folien zum Download unter www.sas.de/lunchtime
WIE HAT IHNEN UNSER WEBINAR GEFALLEN?