SASUnit Examples  Version 1.5.0
generate.sas
Go to the documentation of this file.
1 
26 %MACRO generate(
27  data =
28  ,by =
29  ,out =
30 );
31 
32 /*-- create local data sets and symbols --------------------------------------*/
33 %local d_temp1 d_temp2;
34 data; run; %let d_temp1=&syslast;
35 data; run; %let d_temp2=&syslast;
36 %local i count bycount;
37 
38 /*-- sort input data set and check parameters --------------------------------*/
39 proc sort data=&data out=&d_temp1;
40  by &by;
41 run;
42 %if &syserr %then %do;
43  %put ERROR: Macro Generate: data= or by= specified incorrectly;
44  %return;
45 %end;
46 
47 /*-- determine groups --------------------------------------------------------*/
48 proc means noprint data=&d_temp1(keep=&by);
49  by &by;
50  output out=&d_temp2;
51 run;
52 
53 data _null_;
54  set &d_temp2 nobs=count;
55  call symput ("count", compress(put(count,8.)));
56  stop;
57 run;
58 %do i=1 %to &count;
59  %local label&i;
60 %end;
61 
62 /*-- create data set labels --------------------------------------------------*/
63 data _null_;
64  set &d_temp2 end=eof;
65  array t(1) $ 200 _temporary_;
66  t(1) = 'Dataset for';
67 %let i=1;
68 %do %while(%scan(&by,&i) ne %str());
69  %if &i>1 %then %do;
70  t(1) = trim(t(1)) !! ',';
71  %end;
72  t(1) = trim(t(1)) !! " %scan(&by,&i)=" !! trim(left(vvalue(%scan(&by,&i))));
73  %let i = %eval(&i+1);
74 %end;
75 %let bycount=%eval(&i-1);
76  t(1) = trim(t(1)) !! ' (' !! compress(put(_freq_,8.)) !! ' observations)';
77  call symput ('label' !! compress(put(_n_,8.)), trim(t(1)));
78 run;
79 
80 /*-- create output data sets -------------------------------------------------*/
81 data %do i=1 %to &count; &out&i (label="&&label&i") %end; ;
82  set &d_temp1;
83  by &by;
84  array t(1) _temporary_;
85  if first.%scan(&by,&bycount) then t(1)+1;
86  select(t(1));
87 %do i=1 %to &count;
88  when(&i) output &out&i;
89 %end;
90  end;
91 run;
92 
93 proc datasets lib=work nolist;
94  delete %scan(&d_temp1,2,.) %scan(&d_temp2,2,.);
95 quit;
96 %MEND generate;