标题: 求助:手工建立一个数据集时遇到的问题 - 数据集的转置? [打印本页] 作者: shiyiming 时间: 2012-1-5 10:23 标题: 求助:手工建立一个数据集时遇到的问题 - 数据集的转置? 我现在需要手工建立一个只有8条观测的数据集。
现在的问题是我要建的数据集包含306个变量,变量的类型和长度也不完全相同。我已经有这些变量名字的列表,而且我也把这8条观测的具体的变量值都填好在一个Excel表格里了,而且也import 进 SAS 了。现在需要做的是数据集的转置,这样我就不需要手工输入这8个观测了。
具体地说,我已经有一个数据集,它有306行,9列。我想要把它变成8行,306列,当然,有的变量的值是缺失值。
例如,
var1 23 35 56 76 78 39 09 89
var2 A B C D E E F F
var3 0 1 0 0 2 3 0 1
...
var306 A123 B456 C789 D345 E987 F001 G367 H268
我希望它变成
var1 var2 var3 ... ... var306
23 A 0 A123
35 B 1 B456
56 C 0 C789
76 D 0 D345
78 E 2 E987
39 E 3 F001
09 F 0 G367
89 F 1 H268
谢谢大家!作者: shiyiming 时间: 2012-1-5 12:58 标题: Re: 求助:手工建立一个数据集时遇到的问题 - 数据集的转置? [code:2tf69p9i]proc iml;
use work.had;
read all var _all_ into have0;
close work.had;
tHave0 =have0`;
have1 =tHave0[2:nrow(tHave0), ];
names =tHave0[1, ];
create work.have2 from have1[colname =names];
append from have1;
close have2;
quit;[/code:2tf69p9i]
Hopefully it works.
Jingju作者: shiyiming 时间: 2012-1-6 05:00 标题: Re: 求助:手工建立一个数据集时遇到的问题 - 数据集的转置? [quote="jingju11":27upb1b7][code:27upb1b7]proc iml;
use work.had;
read all var _all_ into have0;
close work.had;
tHave0 =have0`;
have1 =tHave0[2:nrow(tHave0), ];
names =tHave0[1, ];
create work.have2 from have1[colname =names];
append from have1;
close have2;
quit;[/code:27upb1b7]
Hopefully it works.
Jingju[/quote:27upb1b7]
Thank you so much jingju11! Thank you for your time and warmhearted help!
This is a very advanced solution for me, I don't quite understand it actually. But I tried your code and there was an error message.
2 proc iml;
NOTE: IML Ready
3 use my.jan4_8cases;
4 read all var _all_ into have0;
5 close my.jan4_8cases;
6 tHave0 =have0`;
7 have1 =tHave0[2:nrow(tHave0), ];
8 names =tHave0[1, ];
9 create work.have2 from have1[colname =names];
ERROR: Operand requires character value.
statement : CREATE at line 9 column 3
10 append from have1;
ERROR: No data set is currently open for output.
statement : APPEND at line 10 column 3
11 close have2;
NOTE: Cannot close WORK.HAVE2; it is not open.
12 quit;
NOTE: Exiting IML.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE IML used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds作者: shiyiming 时间: 2012-1-6 09:46 标题: Re: 求助:手工建立一个数据集时遇到的问题 - 数据集的转置? sorry about that, i have no idea.
what if you put something like
[code:1twqzcyl]print names;[/code:1twqzcyl]
after defining names matrix.
jingju作者: shiyiming 时间: 2012-1-7 00:27 标题: Re: 求助:手工建立一个数据集时遇到的问题 - 数据集的转置? No problem at all.
Thank you very much jingju11.
I have been able to find another way to manage this and have got it done.
I converted my big Excel table into a .csv file, then made a transform on that .csv file using a Javascript program. However, I have to do some adjustments after in order to make the variable types correct. .csv file assumes all the numbers to be numeric, but I actually need it to be a character variable some time. So a little bit inconvenient, but I got it done finally.
Your program looks very small and powerful. I will try it too.
Thank you so much for your time and helps!作者: shiyiming 时间: 2012-1-7 05:14 标题: Re: 求助:手工建立一个数据集时遇到的问题 - 数据集的转置? Agree with you: the problem may be an issue of when converting excel to sas data; in particular, the ambiguity of some defined variable type when a column was mixed with numeric and char. using .csv through specifying the variable type can avoid (at least alleviate) the problem here for sure. Maybe a more convenient method is to apply option, such as, mixed =yes, in import procedure. The java thing looks like in your expertise but completely new to me.
iml was to transform sas data set mode, which should be independent of importing excel to sas.
jinggu