pingguzh 发表于 2012-11-28 12:21 
从程序的运行扩展性,我觉得bobguy的要好一些,但是我的电脑运行这个程序的时间是2秒左右
pobel的程序在da ...
Unless the performance is an issue, saving a couple of seconds is not much. Simple to understanding and maintenance free are much more important in programming.
Still in this case, the transpose beats the other one by about factor 3. See the log attached below.
157 data test;
158 array x(*) x1-x100;
159 do date=1 to 10000;
160 do j=1 to dim(x);
161 x[j]=ranuni(123);
162 end;
163 output;
164 end;
165 keep date x:;
166 run;
NOTE: The data set WORK.TEST has 10000 observations and 101 variables.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.06 seconds
167
168 %let time=%sysfunc(time( ));
169 data a(keep= date value:) b(keep=date var:) ;
170 set test;
171 array _have (*) x1-x100;
172 array _sorted (*) value1-value100;
173 array _name (*) $ var1-var100;
174
175 *** Sort values;
176 do i=1 to dim(_have);
177 _sorted(i)=_have(i);
178 end;
179 call sortn(value3,value2,value1);
180
181 *** Get variable names;
182 do i=1 to dim(_sorted);
183 do j=1 to dim(_Have);
184 if _have(j)=_sorted(i) then _name(i)=vname(_have(j));
185 end;
186 end;
187 run;
NOTE: There were 10000 observations read from the data set WORK.TEST.
NOTE: The data set WORK.A has 10000 observations and 101 variables.
NOTE: The data set WORK.B has 10000 observations and 101 variables.
NOTE: DATA statement used (Total process time):
real time 3.07 seconds
cpu time 3.07 seconds
188
189 %put >>>>>time of array= %sysevalf(%sysfunc(time( ))-&time)<<<<<;
>>>>>time of array= 3.0779999998922<<<<<
190
191 %let time=%sysfunc(time( ));
192
193 proc transpose data=test out=test2;
194 by date;
195 var x1-x100;
196 run;
NOTE: There were 10000 observations read from the data set WORK.TEST.
NOTE: The data set WORK.TEST2 has 1000000 observations and 3 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
real time 0.20 seconds
cpu time 0.18 seconds
197
198 proc sort data=test2;
199 by date decending col1 ;
200 run;
NOTE: There were 1000000 observations read from the data set WORK.TEST2.
NOTE: The data set WORK.TEST2 has 1000000 observations and 3 variables.
NOTE: PROCEDURE SORT used (Total process time):
real time 0.31 seconds
cpu time 0.73 seconds
201
202 proc transpose data=test2(drop=_name_) out=a(drop=_name_) prefix=value;
203 by date;
204 var col1;
205 run;
NOTE: There were 1000000 observations read from the data set WORK.TEST2.
NOTE: The data set WORK.A has 10000 observations and 101 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
real time 0.22 seconds
cpu time 0.20 seconds
206
207
208 proc transpose data=test2 out=b(drop=_:) prefix=namevar;
209 by date;
210 var _name_ ;
211 run;
NOTE: There were 1000000 observations read from the data set WORK.TEST2.
NOTE: The data set WORK.B has 10000 observations and 101 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
real time 0.36 seconds
cpu time 0.23 seconds
212
213 %put >>>>>time of transpose= %sysevalf(%sysfunc(time( ))-&time)<<<<<;
>>>>>time of transpose= 1.13199999999778<<<<<