-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13 Combining Data Sets.sas
809 lines (659 loc) · 10.6 KB
/
13 Combining Data Sets.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*00 Proc Sort*/
data exsort;
input id age gender $ @@;
datalines;
1 35 M 7 44 F 3 27 M
2 23 M 5 33 F 6 29 F
;
run;
proc print;run; /*why does this work?*/
proc sort data=exsort out=sorted;
/*out= creates a new dataset*/
by id;
run;
title "Data sorted by id";
proc print data=sorted;
run;
title;
proc sort data=exsort out=sort1;
by gender;
run;
title "Unsorted Data";
proc print data=exsort;
run;
title "Data sorted by gender";
proc print data=sort1;
run;
title;
title "Original data";
proc print data=exsort;
run;
proc sort data=exsort;
/*original data are replaced with sorted version*/
by age;
run;
title "Original dataset, after sort by age";
proc print data=exsort;
run;
title;
proc sort data=exsort;
/*original data are replaced with sorted version*/
by age;
run;
title "Original dataset, after sort by age";
proc print data=exsort;
run;
title;
proc sort data=exsort;
by descending age ; /*in reverse order*/
run;
title "Original dataset, sort by age in descending order";
proc print data=exsort;
run;
title;
data exsort;
input id age sbp @@;
datalines;
1 35 122 8 35 118 9 35 135 7 44 121
10 44 115 3 27 122 11 27 114 12 27 118
;
run;
proc print data=exsort;
run;
proc sort data=exsort out=ex1;
by age sbp;
run;
title "Original Data";
proc print data=exsort;
run;
title "Sorted by age and sbp within age";
proc print data=ex1;
run;
title;
proc sort data=exsort out=ex2;
by sbp age;
run;
title "Original Data";
proc print data=exsort;
run;
title "Sorted by sbp and age with sbp";
proc print data=ex2;
run;
Title;
proc sort data=exsort out=ex3;
by descending age sbp;
run;
title "Sorted by descending age and sbp within age";
proc print data=ex3;
run;
title;
/*02 Concatenating SAS Data Sets*/
/*create example data*/
data data1;
input height weight id @@;
datalines;
56.50 98 1 62.25 145 2 62.50 128 3 64.75 119 4 68.75 144 5
;
run;
proc print data=data1 noobs;
title "Data1";
run;
data data2;
input height weight id @@;
datalines;
60 117 6 58 125 7 59 149 8 63 156 9 63 134 10
;
run;
proc print data=data2 noobs;
title "Data2";
run;
title;
data data3;
set data1 data2;
run;
proc print data=data3;run;
/*create example data*/
data data4;
input ht wt id @@;
datalines;
56.50 98 1 62.25 145 2 62.50 128 3 64.75 119 4 68.75 144 5
;
run;
proc print data=data4 noobs;
title "Data4";
run;
data data5;
input height weight id @@;
datalines;
60 117 6 58 125 7 59 149 8 63 156 9 63 134 10
;
run;
proc print data=data5 noobs;
title "Data5";
run;
title;
data data6;
set data4 data5;
run;
proc print data=data6;
title "Data6";
run;
title;
data total;
set data1
data4(rename=(wt=weight ht=height)) data5;
run;
proc print data=total;
run;
data total(rename=(weight=wt height=ht));
set data1
data4(rename=(wt=weight ht=height)) data5;
run;
proc print data=total;
run;
/*five copies of data1*/
data five;
set data1 data1 data1 data1 data1;
run;
proc print data=five noobs;
title "Five copies of data1";
run;
title;
/*03 Proc Append */
data master;
input height weight id @@;
datalines;
56.50 98 1 62.25 145 2 62.50 128 3 64.75 119 4 68.75 144 5
;
run;
proc print data=master noobs;
run;
data newdata;
input height weight id @@;
datalines;
60 117 6 58 125 7 59 149 8 63 156 9 63 134 10
;
run;
proc print data=newdata noobs;
run;
/*Appending a new dataset to an existing dataset*/
proc append base=master data=newdata;
run;
proc print data=master;
run;
proc print data=newdata;
run;
/*master1 and newdata1 have different variables
note the log message*/
data master1;
input height weight chol id @@;
datalines;
56.50 98 234 1
62.25 145 172 2
62.50 128 248 3
64.75 119 215 4
68.75 144 145 5
;
run;
data newdata1;
input height weight id;
datalines;
60 117 6
58 125 7
59 149 8
63 156 9
63 134 10
;
run;
proc append base=master1 data=newdata1;
run;
proc print data=master1;
run;
/*note difference in log message*/
data m1;
set master1;
run;
data nd1;
set newdata1;
run;
proc append base=nd1 data=m1;/*note reversal or roles*/
run;
proc print data=nd1;
run;
data newdata2;
input height weight chol id @@;
datalines;
56.50 98 234 1 62.25 145 172 2 62.50 128 248 3
64.75 119 215 4 68.75 144 145 5
;
run;
data master2;
input height weight id @@;
datalines;
60 117 6 58 125 7 59 149 8 63 156 9 63 134 10
;
run;
proc append base=master2 data=newdata2 force;
run;
proc print data=master2;
run;
/*04 Merging One-to-One*/
data htwt;
input height weight id @@;
datalines;
56.50 98 1 62.25 145 2 62.50 128 3 64.75 119 4
68.75 144 5 60.00 117 6 58.00 125 7
;
run;
data chol;
input chol id @@;
datalines;
234 1 172 2 248 3 215 4
145 5 281 6 335 7
;
run;
proc print data=htwt noobs;
run;
proc print data=chol noobs;
run;
/*positional merging*/
data positionalmerge;
merge htwt chol;
run;
proc print data=positionalmerge;
run;
proc sort data=htwt;
by id;
run;
proc sort data=chol;
by id;
run;
data total;
merge htwt chol;
by id;
run;
proc print data=htwt;
run;
proc print data=chol;
run;
proc print data=total;
run;
data htwt;
input height weight id @@;
datalines;
56.50 98 1 62.50 128 3 64.75 119 4 68.75 144 5
60.00 117 6 63.00 156 9 63.00 134 10
;
run;
data chol;
input chol id @@;
datalines;
215 1 145 2 281 3 335 4 196 7
;
run;
proc print data=htwt noobs;
run;
proc print data=chol noobs;
run;
data total;
merge htwt chol;
by id;
proc print data=total;
run;
/*forgotten by variable*/
data total;
merge htwt chol;
run;
proc print data=htwt;run;
proc print data=chol;run;
proc print data=total;
run;
data htwt;
input height weight id @@;
datalines;
56.50 98 1 62.25 145 2 62.50 128 3 64.75 119 4 68.75 144 5 60.00 117 6 63.00 156 9 63.00 134 10
;
run;
data chol;
input chol id @@;
datalines;
215 1 145 2 281 3 335 4 196 7
;
run;
data bp;
input dbp sbp id @@;
datalines;
83 125 1 73 108 4 71 108 5 79 116 6
89 170 7 80 120 8 70 108 9 79 123 10
;
run;
data total;
merge htwt chol bp;
by id;
run;
proc print data=total;
run;
proc sort data=bp;
by id;
run;
proc sort data=htwt;
by id;
run;
proc sort data=chol;
by id;
run;
data total;
merge bp htwt chol;
by id;
run;
proc print data=bp noobs;
run;
proc print data=htwt noobs;
run;
proc print data=chol noobs;
run;
proc print data=total noobs ;
run;
data tmp1;
input x @@;
id=_n_;
datalines;
1 1 1 1 1
;
data tmp2;
input x @@;
id=_n_;
datalines;
2 2 2 2 2
;
proc print data=tmp1;
run;
proc print data=tmp2;
run;
proc sort data=tmp1;
by id;
run;
proc sort data=tmp2;
by id;
run;
data tot1;
merge tmp2 tmp1;
by id;
run;
proc print data=tot1 noobs;
run;
data tmp1;
input x;
id=_n_;
datalines;
1
1
1
1
1
;
data tmp2;
input x;
id=_n_;
datalines;
2
2
2
2
2
;
proc sort data=tmp1;
by id;
run;
proc sort data=tmp2;
by id;
run;
/*note difference from version 1*/
data tot2;
merge tmp1 tmp2;
by id;
run;
proc print data=tot2 noobs;
run;
data htwt;
input height weight id;
datalines;
56.50 98 1
62.25 145 2
62.50 128 3
64.75 119 4
68.75 144 5
60.00 117 6
63.00 156 9
63.00 134 10
;
run;
data chol;
input chol id1;
datalines;
215 1
145 2
281 3
335 4
196 7
;
run;
proc sort data=chol;
by id1;
run;
proc sort data=htwt;
by id;
run;
data total;
merge chol(rename =(id1=id)) htwt;
by id;
proc print data=total;
run;
data chol;
input chol id1;
datalines;
215 1
145 2
281 3
335 4
196 7
;
run;
data chol;
set chol;
rename id1=id;/*this is the last thing done by data step*/
run;
proc sort data=chol;
by id;
run;
proc sort data=htwt;
by id;
run;
data total;
merge chol htwt;
by id;
proc print data=total;
run;
data htwt;
input height weight id;
datalines;
56.50 98 1
62.25 145 2
62.50 128 3
64.75 119 4
68.75 144 5
60.00 117 6
63.00 156 9
63.00 134 10
;
run;
data chol;
input chol id;
datalines;
215 1
145 2
281 3
335 4
196 7
;
run;
********** IN= Option Matches Only **********;
data total;
merge htwt(in=onhtwt)
chol(in=onchol);
by id;
/*intersection of htwt and chol*/
if onhtwt=1 and onchol=1;
run;
proc print data=htwt;
run;
proc print data=chol;
run;
proc print data=total;
run;
data htwt;
input height weight id;
datalines;
56.50 98 1
62.25 145 2
62.50 128 3
64.75 119 4
68.75 144 5
60.00 117 6
63.00 156 9
63.00 134 10
;
run;
data chol;
input chol id;
datalines;
215 1
145 2
281 3
335 4
196 7
;
run;
********** IN= Option non-Matches Only **********;
data total;
merge htwt(in=onhtwt)
chol(in=onchol);
by id;
/*observations only in one data set*/
if onhtwt ne 1 or onchol ne 1;
run;
proc print data=htwt;
run;
proc print data=chol;
run;
proc print data=total;
run;
data htwt;
input height weight id;
datalines;
56.50 98 1
62.25 145 2
62.50 128 3
64.75 119 4
68.75 144 5
60.00 117 6
63.00 156 9
63.00 134 10
;
run;
data chol;
input chol id;
datalines;
215 1
145 2
281 3
335 4
196 7
;
run;
********** IN= Option to create three datasets **********;
data onboth htwtonly cholonly;
merge htwt(in=onhtwt)
chol(in=onchol);
by id;
if onhtwt = 1 and onchol = 1 then output onboth;
if onhtwt =1 and onchol =0 then output htwtonly;
if onhtwt=0 and onchol=1 then output cholonly;
run;
proc print data=onboth noobs;
run;
proc print data=cholonly noobs;
run;
proc print data=htwtonly noobs;
run;
data htwt;
input height weight id;
datalines;
56.50 98 1
62.25 145 2
62.50 128 3
64.75 119 4
68.75 144 5
60.00 117 6
63.00 156 9
63.00 134 10
;
run;
data chol;
input chol id;
datalines;
215 1
145 2
281 3
335 4
196 7
;
run;
********** IN= Option to tabulate completeness **********;
data total;
merge htwt(in=onhtwt)
chol(in=onchol);
by id;
onht=onhtwt;
onch=onchol;
run;
proc freq data=total;
tables onht*onch/norow nocol nopercent;
run;
/*05 Interleaving SAS Data Sets*/
data tmp1;
length id height weight 8;
input height weight id @@;
datalines;
56.50 98 1 62.25 145 2 62.50 128 3 64.75 119 4 68.75 144 5
60.00 117 6 58.00 125 7 59.00 149 8 63.00 156 9 63.00 134 10
58.00 105 11 65.75 151 12 64.50 185 13 67.50 144 14 59.25 156 15
63.00 186 16 65.25 128 17 64.50 166 18 62.00 156 19 61.25 137 20
63.25 150 21 61.00 142 22 58.50 131 23 63.50 182 24 61.50 107 25
60.50 96 26 62.50 137 27 63.75 136 28 64.50 133 29 65.25 149 30
66.25 137 31 59.00 95 32 65.75 138 33 57.50 165 34 69.00 162 35
63.00 156 36 62.00 168 37 62.25 195 38 61.50 131 39 60.75 165 40
60.75 122 41 62.50 142 42 62.00 106 43 58.50 136 44 62.25 127 45
;
run;
proc print data=tmp1 noobs;
run;
data odd even;
set tmp1;
if mod(id,2) = 0 then output even;
else output odd;
run;
proc print data=odd noobs;
title "Odd numbered id's";
run;
proc print data=even noobs;
title "Even numbered id's";
run;
data tot;
set odd even;
by id;
run;
proc print data=tot noobs;
title "Data sets odd and even, interleaved";
run;
title;