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