fattyclouds 发表于 2014-2-13 02:01 
我还有一个问题想请教,如果我选择的下标变量y里有重复值,比如说y是1,3,6,6,最后通过proc sql产生的 ...
First Proc SQL is fine. It selected all the columns as you can see in the log, but there can't be duplicate column names in a SAS data set, so duplicates were not selected in the second SQL. Here is a solution that might be close to what you want.
SAS LOG
6977 proc sql noprint;
6978 select selectedindex into: selectedindex separated by ','
6979 from index;
6980 %put &selectedindex;
x1,x3,x4,x4,x6,x6,x6
6981 create table selecteddata as
6982 select &selectedindex
6983 from data;
WARNING: Variable x4 already exists on file WORK.SELECTEDDATA.
WARNING: Variable x6 already exists on file WORK.SELECTEDDATA.
WARNING: Variable x6 already exists on file WORK.SELECTEDDATA.
NOTE: Table WORK.SELECTEDDATA created, with 10 rows and 4 columns.
6984 quit;
NEW CODE
data data;
input x1 x2 x3 x4 x5 x6 x7 x8 x9 x10;
datalines;
2 3 5 6 9 9 5 5 4 1
2 2 4 7 6 3 5 2 4 11
1 6 8 2 1 2 9 6 6 14
5 3 2 4 6 0 1 2 5 3
2 8 7 6 2 5 5 7 3 2
2 3 5 6 9 9 5 5 4 1
5 3 4 1 3 4 6 7 3 9
6 0 1 6 2 5 1 3 4 6
2 3 5 6 9 9 5 5 4 1
5 5 1 3 6 2 2 4 7 3
;
run;
data index;
input index;
datalines;
1
3
6
4
6
4
6
;
run;
data index;
set index;
order+1;
run;
proc sort data=index ;
by index;
run;
data index;
set index;
by index;
selectedindex=compress('x'||index);
if first.index then count=1;
else count+1;
run;
data index;
set index;
length sql $30;
if count=1 then sql=selectedindex;
else sql=selectedindex||' as '||compress(selectedindex||'_'||count);
run;
proc sql noprint;
select sql into: selectedindex separated by ','
from index
order by order;
%put &selectedindex;
create table selecteddata as
select &selectedindex
from data;
quit;