Die Beispiele verwenden generierte Daten (Datalines), um die für die Demonstration der Datenverschachtelung erforderlichen SAS-Datasets zu erstellen.
1 Codeblock
DATA STEP / PROC SORT Data
Erklärung : Dieses Beispiel erstellt zwei Datasets, 'animal' und 'plant', und sortiert sie nach der gemeinsamen Variablen 'common'. Anschließend verschachtelt der DATA-Schritt diese Datasets mithilfe der BY-Anweisung. Das Ausgabe-Dataset 'interleave' enthält die Beobachtungen beider Datasets, geordnet nach 'common'.
Kopiert!
data animal;
input common $ animal $;
datalines;
a Ant
b Bird
c Cat
d Dog
e Eagle
f Frog
;
run;
data plant;
input common $ plant $;
datalines;
a Apple
b Banana
c Coconut
d Dewberry
e Eggplant
f Fig
;
run;
proc sort data=animal; by common; run;
proc sort data=plant; by common; run;
data interleave;
set animal plant;
by common;
run;
proc print data=interleave; run;
1
DATA animal;
2
INPUT common $ animal $;
3
DATALINES;
4
a Ant
5
b Bird
6
c Cat
7
d Dog
8
e Eagle
9
f Frog
10
;
11
RUN;
12
13
DATA plant;
14
INPUT common $ plant $;
15
DATALINES;
16
a Apple
17
b Banana
18
c Coconut
19
d Dewberry
20
e Eggplant
21
f Fig
22
;
23
RUN;
24
25
PROC SORTDATA=animal; BY common; RUN;
26
PROC SORTDATA=plant; BY common; RUN;
27
28
DATA interleave;
29
SET animal plant;
30
BY common;
31
RUN;
32
PROC PRINTDATA=interleave; RUN;
2 Codeblock
DATA STEP / PROC SORT Data
Erklärung : Dieses Beispiel demonstriert die Verschachtelung von Datasets ('animalDupes' und 'plantDupes'), die doppelte Werte für die BY-Variable 'common' enthalten. Die Datasets werden vor der Verschachtelung sortiert. Die Reihenfolge der Datasets in der SET-Anweisung beeinflusst die Reihenfolge der Beobachtungen mit denselben 'common'-Werten im Ausgabe-Dataset. Ein zusätzliches Beispiel mit der Reihenfolge 'plantDupes animalDupes' wird zur Veranschaulichung dieses Effekts bereitgestellt.
Kopiert!
data animalDupes;
input common $ animal $;
datalines;
a Ant
a Ape
b Bird
c Cat
d Dog
e Eagle
;
run;
data plantDupes;
input common $ plant $;
datalines;
a Apple
b Banana
c Coconut
c Celery
d Dewberry
e Eggplant
;
run;
proc sort data=animalDupes; by common; run;
proc sort data=plantDupes; by common; run;
data interleave;
set animalDupes plantDupes;
by common;
run;
proc print data=interleave; run;
1
DATA animalDupes;
2
INPUT common $ animal $;
3
DATALINES;
4
a Ant
5
a Ape
6
b Bird
7
c Cat
8
d Dog
9
e Eagle
10
;
11
RUN;
12
13
DATA plantDupes;
14
INPUT common $ plant $;
15
DATALINES;
16
a Apple
17
b Banana
18
c Coconut
19
c Celery
20
d Dewberry
21
e Eggplant
22
;
23
RUN;
24
25
PROC SORTDATA=animalDupes; BY common; RUN;
26
PROC SORTDATA=plantDupes; BY common; RUN;
27
28
DATA interleave;
29
SET animalDupes plantDupes;
30
BY common;
31
RUN;
32
33
PROC PRINTDATA=interleave; RUN;
3 Codeblock
DATA STEP / PROC SORT Data
Erklärung : Dieses Beispiel veranschaulicht die Verschachtelung von Datasets ('animalDupes' und 'plantMissing2'), bei denen die BY-Variable 'common' Werte enthält, die in einem Dataset vorhanden sind, aber nicht im anderen (z. B. 'd' in 'animalDupes' und 'f' in 'plantMissing2'). Nach dem Sortieren verschachtelt der DATA-Schritt die Datasets. Variablen, die in einer bestimmten Beobachtung eines Eingabe-Datasets nicht vorhanden sind, erhalten im Ausgabe-Dataset fehlende Werte.
Kopiert!
data animalDupes;
input common $ animal $;
datalines;
a Ant
a Ape
b Bird
c Cat
d Dog
e Eagle
;
run;
data plantMissing2;
input common $ plant $;
datalines;
a Apple
b Banana
c Coconut
e Eggplant
f Fig
;
run;
proc sort data=animalDupes; by common; run;
proc sort data=plantMissing2; by common; run;
data interleave;
set animalDupes plantMissing2;
by common;
run;
proc print data=interleave; run;
1
DATA animalDupes;
2
INPUT common $ animal $;
3
DATALINES;
4
a Ant
5
a Ape
6
b Bird
7
c Cat
8
d Dog
9
e Eagle
10
;
11
RUN;
12
13
DATA plantMissing2;
14
INPUT common $ plant $;
15
DATALINES;
16
a Apple
17
b Banana
18
c Coconut
19
e Eggplant
20
f Fig
21
;
22
RUN;
23
24
PROC SORTDATA=animalDupes; BY common; RUN;
25
PROC SORTDATA=plantMissing2; BY common; RUN;
26
27
DATA interleave;
28
SET animalDupes plantMissing2;
29
BY common;
30
RUN;
31
32
PROC PRINTDATA=interleave; RUN;
Dieses Material wird von We Are Cas "wie besehen" zur Verfügung gestellt. Es gibt keine ausdrücklichen oder stillschweigenden Garantien hinsichtlich der Marktgängigkeit oder Eignung für einen bestimmten Zweck in Bezug auf die hierin enthaltenen Materialien oder Codes. We Are Cas ist nicht verantwortlich für Fehler in diesem Material, wie es jetzt existiert oder existieren wird, noch bietet We Are Cas technischen Support dafür an.
SAS und alle anderen Produkt- oder Dienstleistungsnamen von SAS Institute Inc. sind eingetragene Marken oder Marken von SAS Institute Inc. in den USA und anderen Ländern. ® zeigt die Registrierung in den USA an. WeAreCAS ist eine unabhängige Community-Site und nicht mit SAS Institute Inc. verbunden.
Diese Website verwendet technische und analytische Cookies, um Ihre Erfahrung zu verbessern.
Mehr erfahren.