D*o 发帖数: 210 | 1 先多谢大家。
我有一个文件,其中一个variable name是:Firms,
Firms的值可以是F1, F2,F3,.........Fn;
我现在想要让sas自动create n个文件,并且指定文件名就是Firms 这个变量的value,
然后每个observation根据他们Firms这个变量的值分配到相应的文件里。麻烦指点一下
具体应该怎么做,或者应该用什么命令。 |
d******9 发帖数: 404 | 2 Use macro.
sas自动create n个文件, 什么文件??? If SAS datasets, easy. If other
application files, maybe need use X statement. |
s*******e 发帖数: 1385 | 3 %macro test;
proc sql;
select distinct firms into: firmvars separated by ' '
from yourdata;
quit;
%do i=1 %to %sysfunc(countw(&firmvars));
%let firmvar=%scan(&firmvars., &i., ' ');
data &firmvar.;
set yourdata;
where Firms="&firmvar.";
run;
%end;
%mend test;
%test
【在 D*o 的大作中提到】 : 先多谢大家。 : 我有一个文件,其中一个variable name是:Firms, : Firms的值可以是F1, F2,F3,.........Fn; : 我现在想要让sas自动create n个文件,并且指定文件名就是Firms 这个变量的value, : 然后每个observation根据他们Firms这个变量的值分配到相应的文件里。麻烦指点一下 : 具体应该怎么做,或者应该用什么命令。
|
D*o 发帖数: 210 | 4 没有说清楚,就是datasets 谢谢
【在 d******9 的大作中提到】 : Use macro. : sas自动create n个文件, 什么文件??? If SAS datasets, easy. If other : application files, maybe need use X statement.
|
d******9 发帖数: 404 | 5 This macro works, but it is not a good macro.
如果楼主需要的不是 SAS datasets 呢?
【在 s*******e 的大作中提到】 : %macro test; : proc sql; : select distinct firms into: firmvars separated by ' ' : from yourdata; : quit; : %do i=1 %to %sysfunc(countw(&firmvars)); : %let firmvar=%scan(&firmvars., &i., ' '); : data &firmvar.; : set yourdata; : where Firms="&firmvar.";
|
d******9 发帖数: 404 | 6 Then it is very easy
%macro AAA;
proc sql;
select conunt(distinct Firms) into : N
from Input_Table;
%let N=&N;
select distinct strip(Firms) into : F_1 - : F_&N
from Input_Table;
quit;
%local I;
%do I=1 %to &N;
data &&F_&I;
set Input_Table;
where Firms="&&F_&I";
run;
%end;
%mend;
【在 D*o 的大作中提到】 : 没有说清楚,就是datasets 谢谢
|
D*o 发帖数: 210 | 7 多谢 我试试看。
【在 d******9 的大作中提到】 : Then it is very easy : %macro AAA; : proc sql; : select conunt(distinct Firms) into : N : from Input_Table; : %let N=&N; : select distinct strip(Firms) into : F_1 - : F_&N : from Input_Table; : quit; : %local I;
|
k*******a 发帖数: 772 | 8 there are many ways to do this, here is my solution:
data MyData;
input firm $ x;
datalines;
F1 1
F2 3
F3 5
F3 6
F1 9
F2 99
F6 3
;
run;
proc sql noprint;
select distinct firm into :data separated by ' ' from MyData;
select distinct "if firm ='"||strip(firm)||"' then output "|| firm into
:code separated by '; ' from MyData;
quit;
data &data;
set MyData;
&code;
run; |
d******9 发帖数: 404 | 9 Yes, this is another way, without using macro do-loop.
But, if the input data set is huge and the variable Firms has thousands of
levels, then this method is not efficient, esp. when you use a lot IF... IF
rather than IF-ELSE IF.
【在 k*******a 的大作中提到】 : there are many ways to do this, here is my solution: : data MyData; : input firm $ x; : datalines; : F1 1 : F2 3 : F3 5 : F3 6 : F1 9 : F2 99
|
k*******a 发帖数: 772 | 10 这个改下code不难
改成 separated by ';else ' 就可以了
IF
【在 d******9 的大作中提到】 : Yes, this is another way, without using macro do-loop. : But, if the input data set is huge and the variable Firms has thousands of : levels, then this method is not efficient, esp. when you use a lot IF... IF : rather than IF-ELSE IF.
|
D*o 发帖数: 210 | 11 多谢
IF
【在 d******9 的大作中提到】 : Yes, this is another way, without using macro do-loop. : But, if the input data set is huge and the variable Firms has thousands of : levels, then this method is not efficient, esp. when you use a lot IF... IF : rather than IF-ELSE IF.
|