我正在使用SAS,我想创建一个指标变量。
我拥有的数据是这样的(我拥有的数据):
,我想将其更改为(DATA I WANT):
我要使用固定的总时间,并且starttime具有重复的时间值(在此示例中,c1和c2都在时间3开始)。尽管我使用的示例很小,只有5个名称和12个时间值,但实际数据却非常大(大约40,000个名称和100,000个时间值-所以我想要的结果是一个100,000x40,000的矩阵。)
有人可以提供有关如何处理此问题的提示/解决方案吗?
答案 0 :(得分:0)
这将起作用。可能有一个更简单的解决方案,可以在一个数据步骤中完成所有这些操作。我的数据步骤创建了一个交错的结果,必须对其进行折叠,这是我对排序/平均值求和的结果。
data have;
input starttime name $;
datalines;
3 c1
3 c2
5 c3
10 c4
11 c5
;
run;
data want(drop=starttime name);
set have;
array cols (*) c1-c5;
do time=1 to 100;
if starttime < time then cols(_N_)=1;
else cols(_N_)=0;
output;
end;
run;
proc sort data=want;
by time;
proc means data=want noprint;
by time;
var _numeric_;
output out=want2(drop=_type_ _freq_) sum=;
run;
我不建议您这样做。您没有提供足够的信息让我们知道您为什么想要这样大小的矩阵。您可能在运行它时遇到处理问题。
在do time=1 to 100
行中,您可以将其更改为100000或任何长度。
答案 1 :(得分:0)
40k变量很多。看到这种扩展的程度会很有趣。您如何确定停止时间?
data have;
input starttime name :$32.;
retain one 1;
cards;
1 varx
3 c1
3 c2
5 c3x
10 c4
11 c5
;;;;
run;
proc print;
run;
proc transpose data=have out=have2(drop=_name_ rename=(starttime=time));
by starttime;
id name;
var one;
run;
data time;
if 0 then set have2(drop=time);
array _n[*] _all_;
retain _n 0;
do time=.,1 to 12;
output;
call missing(of _n[*]);
end;
run;
data want0 / view=want0;
merge time have2;
by time;
retain dummy '1';
run;
data want;
length time 8;
update want0(obs=0) want0;
by dummy;
if not missing(time);
output;
drop dummy;
run;
proc print;
run;
答案 2 :(得分:0)
我认为下面的代码会起作用:
%macro answer_macro(data_in, data_out);
/* Deduplication of initial dataset just to assure that every variable has a unique starting time*/
proc sort data=&data_in. out=data_have_nodup; by name starttime; run;
proc sort data=data_have_nodup nodupkey; by name; run;
/*Getting min and max starttime values - here I am assuming that there is only integer values form starttime*/
proc sql noprint;
select min(starttime)
,max(starttime)
into :min_starttime /*not used. Use this (and change the loop on the next dataset) to start the time variable from the value where the first variable starts*/
,:max_starttime
from data_have_nodup
;quit;
/*Getting all pairs of name/starttime*/
proc sql noprint;
select name
,starttime
into :name1 - :name1000000
,:time1 - :time1000000
from data_have_nodup
;quit;
/*Getting total number of variables*/
proc sql noprint;
select count(*) into :nvars
from data_have_nodup
;quit;
/* Creating dataset with possible start values */
/*I'm not sure this step could be done with a single datastep, but I don't have SAS
on my PC to make tests, so I used the method below*/
data &data_out.;
do i = 1 to &max_starttime. + 1;
time = i; output;
end;
drop i;
run;
data &data_out.;
set &data_out.;
%do i = 1 %to &nvars.;
if time >= &&time&i then &&name&i = 1;
else &&name&i = 0;
%end;
run;
%mend answer_macro;
很遗憾,我的机器上现在没有SAS,因此无法确认代码是否有效。但是,即使没有,您也可以使用其中的逻辑。