请问高手,我的数据如下aaa,现在我要在d 与e之间插入一个变量p,要怎样写sas程序?不能在程序中把aaa原先的所有变量全都列出来,如下proc sql 程序步,因为有可能原先变量很多,或者原先变量的名称复杂,谢谢高手指点!
proc sql;
create table ccc as
select a b c d p e f g h;
from aaa;
run;
data aaa ;
input a b c d e f g h;
cards;
1 2 3 4 5 6 7 8
;
run;
Below program could be tried, but it is hard to say which is more efficient without test.
data aaa;
input a b c d e f g h;
cards;
1 2 3 4 5 6 7 8
;
run;
data ddd;
set aaa(obs=0 keep=a--d);
p=.;
set aaa(obs=0 keep=e--h);
run;
proc append base=ddd data=aaa force;
run;
If p from other dataset, try to this.
data ddd;
merge aaa (keep=a--d) ccc(keep=p) a ...
放在后面不行吗?这是放在后面的方法:
data join;
set table1;
set table2;
run;
table1是存储a b c d e f g h的数据集,table2是存储p的数据集。
在分析过程中变量的顺序不太重要。当然了,如果变量名有规律的话,可以进行排序。要不你就在excel里操作一下。
希望能帮当你。
data aaa;
input a b c d e f g h;
cards;
1 2 3 4 5 6 7 8
;
proc sql;
create table ccc as
select a, b,c,d, sum(a) as p, 1 as pp, e, f, g, h
from aaa;
quit;
proc print data=ccc; run;
data aaa;
ID = _N_;
input a b c d e f g h;
cards;
1 2 3 4 5 6 7 8
;
proc sql;
create table bbb(drop=id) as
select a.*, (a.a + b.e) as sum_ae, b.*
from aaa (drop=e--h) as a, aaa(drop=a--d) as b
where a.id=b.id;
quit;
或者
data ccc;
input a b c d e f g h;
cards;
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
;
proc sql noprint;
select name into: a_e separated by ', '
from dictionary.columns
where libname='WORK' and memname='CCC' and varnum in (1 2 3 4);
select name into: e_h separated by ', '
from dictionary.columns
where libname='WORK' and memname='CCC' and varnum in (5 6 7 8);
quit;
proc sql;
select &a_e, sum(d) as sum_d, &e_h
from ccc;
quit;
或者用 SQL 和 data step
proc sql noprint;
select name into: a__e separated by ' '
from dictionary.columns
where libname='WORK' and memname='CCC' and varnum in (1 2 3 4);
select name into: e__h separated by ' '
from dictionary.columns
where libname='WORK' and memname='CCC' and varnum in (5 6 7 8);
quit;
data ddd;
retain &a__e sum_de &e__h;
set ccc;
sum_de = d+e;
run;
谢谢各位高手!谢谢imasasor,虽然imassor的程序很好,但是imasasor没考虑到a b c d e f g h只是随机顺序,也有可能是a bb cc d e ff gg h,这时imasasor程序就用不了,但是这时farmman60的程序就可以用。故我将他的答案设为最佳答案!不过还是谢谢各位高手指导!!