Sas 将一个变量的级别转换为其他变量
我有一个类似以下内容的数据集:Sas 将一个变量的级别转换为其他变量,sas,Sas,我有一个类似以下内容的数据集: IDnum State Product Consumption 123 MI A 30 123 MI B 20 123 MI C 45 456 NJ A 15 456 NJ D 10 789 MI B 60 .
IDnum State Product Consumption
123 MI A 30
123 MI B 20
123 MI C 45
456 NJ A 15
456 NJ D 10
789 MI B 60
... ... ... ...
我想创建一个新的数据集,其中每个IDnum有一行,每个不同的产品有一个新的虚拟变量(在我的真实数据集中,我有将近1000个产品),以及相关的消费。看起来像是这样的
IDnum State Prod.A Cons.A Prod.B Cons.B Prod.C Cons.C Prod.D Cons.D
123 MI yes 30 yes 20 yes 45 no -
456 NJ yes 15 no - no - yes 10
789 MI no - yes 60 no - no -
... ... ... ... ... ... ... ... ... ...
某些变量(如“State”)在同一个IDnum中不会发生变化,但原始银行中的每一行都相当于一次购买,因此相同IDnum的“product”和“consumpion”变量会发生变化。我希望我的新数据集能在一行中显示每个消费者的所有消费习惯,但到目前为止我失败了
非常感谢您的任何帮助。如果没有是/否变量,这真的很容易:
data input;
length State $2 Product $1;
input IDnum State Product Consumption;
cards;
123 MI A 30
123 MI B 20
123 MI C 45
456 NJ A 15
456 NJ D 10
789 MI B 60
;
run;
proc transpose data=input out=output(drop=_NAME_) prefix=Cons_;
var Consumption;
id Product;
by IDnum State;
run;
添加是/否字段:
proc sql;/* from column names or alternatively
create it from source data directly if not taking too long */
create table work.products as
select scan(name, 2, '_') as product length=1
from dictionary.columns
where libname='WORK' and memname='OUTPUT'
and upcase(name) like 'CONS_%';
quit;
filename vars temp;/* write a temp file containing variable definitions
in desired order */
data _null_;
set work.products end=last;
file vars;
length str $40;
if _N_ = 1 then put 'LENGTH ';
str = catt('Prod_', product, ' $3');
put str;
str = catt('Cons_', product, ' 8');
put str;
if last then put ';';
run;
options source2;
data output2;
length IdNum 8 State $2;
%include vars;
set output;
array prod{*} Prod_:;
array cons{*} Cons_:;
drop i;
do i=1 to dim(prod);
if coalesce(cons(i), 0) ne 0 then prod(i)='yes';
else prod(i)='no';
end;
run;
我很想知道为什么您的数据中需要有“是/否”列,鉴于您拥有的产品数量,这将创建大量额外数据。如果您执行@vasja建议的简单换位,那么很容易从Cons列中查看/计算该ID是否使用了该产品