Date: Fri, 22 Nov 2002 17:45:13 -0500
Reply-To: Paul McDonald <pdm@SPIKEWARE.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Paul McDonald <pdm@SPIKEWARE.COM>
Subject: Re: Set statement bug?
Paul Dorfman shows some good ideas here!
I've got a suggestion on the first one that I like...
1 /*
2 Imagine you have 3 SAS data sets A1-A3; you want to know which
one has
3 the largest number of records and print that number in the log.
Here is
4 one way of going about it:
5 */
6
7 /* Create a sample data set */
8
9 data a b c ;
10 do i = 1 to 100 ;
11 if i < 25 then output a ;
12 else output b ;
13 output c ;
14 end ;
15 run ;
NOTE: The data set WORK.A has 24 observations and 1 variables.
NOTE: The data set WORK.B has 76 observations and 1 variables.
NOTE: The data set WORK.C has 100 observations and 1 variables.
NOTE: DATA statement used:
real time 0.05 seconds
cpu time 0.04 seconds
16
17 /* Paul Dorfman's Method */
18
19 data _null_ ;
20 max = na1 max na2 max na3 ;
21 put max = ;
2 The SAS
System 15:27 Friday, November 22, 2002
22 stop ;
23 set a nobs = na1 ;
24 set b nobs = na2 ;
25 set c nobs = na3 ;
26 run ;
max=100
NOTE: DATA statement used:
real time 0.04 seconds
cpu time 0.04 seconds
27
28 /* Paul McDonald's Method, using %obscnt macro from stored macro
facility
29
30 %macro obscnt (data) /des='returns number obs from a SAS
dataset' ;
31 %local data data_id rc ;
32
33 %let data_id = %sysfunc(open(&data)) ;
34 %if &data_id %then %do ;
35 %sysfunc(attrn(&data_id, nobs))
36 %end ;
37 %else %do ;
38 %put WARNING: Open for dataset %data(&data) failed ;
39 %put WARNING: Macro OBSCNT will return the number of
observations as missing. ;
40 %put %sysfunc(sysmsg()) ;
41 .
42 %end ;
43 %let rc = %sysfunc(close(&data_id)) ;
44 %mend obscnt ;
45 */
46
47 data _null_ ;
48 max = max(%obscnt(a), %obscnt(b), %obscnt(c)) ;
49 put max = ;
50 run ;
max=100
NOTE: DATA statement used:
real time 0.06 seconds
cpu time 0.04 seconds