楼主: funwin
5067 30

请教 数据导入问题 [推广有奖]

11
jingju11 发表于 2009-8-18 07:49:26

不可能啊, 变量名无法带有‘-’字符啊。

10# funwin
也就是说 这两个表格已经是sas 格式的表格了
VARIABLE 1 1999-1-30 1999-2-29 1999-3-31 ……
Com1 0.9 0.2 0.7 ……
Com2 0.4 0.5 0.3 …….
Com3 0.88 0.89 0.92 …….

也就是说,在sas表里,红色的一行是sas 的变量名。对吧

12
jingju11 发表于 2009-8-18 10:58:23
11# jingju11

PROC IMPORT OUT= WORK.tab1
            DATAFILE= "C:\Documents and Settings\desheng\My Files\Book1.xls"
            DBMS=EXCEL REPLACE;
     RANGE="Sheet1$";
     GETNAMES=YES;
     MIXED=NO;
     SCANTEXT=YES;
     USEDATE=YES;
     SCANTIME=YES;
RUN;
PROC IMPORT OUT= WORK.tab2
            DATAFILE= "C:\Documents and Settings\desheng\My Files\Book2.xls"
            DBMS=EXCEL REPLACE;
     RANGE="Sheet1$";
     GETNAMES=YES;
     MIXED=NO;
     SCANTEXT=YES;
     USEDATE=YES;
     SCANTIME=YES;
RUN;
data tab1_;
set tab1;
array dt{*} _:;
do i=1 to dim(Dt);
  company=variable_;
  date_=input(substrn(vname(Dt{i}),2), yymmdd10.);
  var1=dt{i};
  output;
end;
format date_ date9.;
keep company date_ var1;
run;
data tab2_;
set tab2;
array dt{*} _:;
do i=1 to dim(Dt);
  company=variable_;
  date_=input(substrn(vname(Dt{i}),2), yymmdd10.);
  var2=dt{i};
  output;
end;
format date_ date9.;
keep company date_ var2;
run;
proc sort data=tab1_;by company date_;
run;
proc sort data=tab2_;by company date_;
run;
data all;
merge tab1_ tab2_;
by company date_;
run;

Book1.xls
下载链接: https://bbs.pinggu.org/a-382785.html

13.5 KB

Book2.xls

13.5 KB

13
funwin 发表于 2009-8-18 20:31:00
12# jingju11

非常感谢!用您的方法 可以解决问题了! 并且用你的数据也可以得出结果。 但是 用的我自己的数据上,怎么第一列 第二列 都是缺失值  只有第三列 才有数值?
估计是 数值的 格式 问题。
附上 我的数据book1(截取部分),希望 能帮我看看 为什么 我的数据就 得不出结果呢?

book1.xls

14 KB

14
funwin 发表于 2009-8-18 20:49:06
又调整了一下 可以出现 第一列公司代码  但是 第二列 日期 还是确实值
是否是日期格式问题?或者 这个语句不适合
date=input(substrn(vname(dt{i}),2), yymmdd10.);

15
funwin 发表于 2009-8-18 21:08:21
又发现 日期只有在2000-01-31以后的数据可以显示出来, 之前的数据 是缺失值, 为什么??
code        date        ret
902792                -0.028168912
902792                -0.144926998
902792                0.338981578
902792                0.164556428
902792                -0.163043024
902792                -0.090908788
902792                -0.157142282
902792                -0.033898158
902792                -0.017543781
902792                0
902792                0.071428244
902792                0.099999573
902792                0
902792                0.030302913
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792                0
902792        2000-1-31        0
902792        2000-2-29        0
902792        2000-3-31        0
902792        2000-4-28        0
902792        2000-5-31        0
902792        2000-6-30        0
902792        2000-7-31        0
902792        2000-8-31        0
902792        2000-9-29        0
902792        2000-10-31        0
902792        2000-11-30        0
902792        2000-12-29        0
902792        2001-1-31        0
902792        2001-2-28        0
902792        2001-3-30        0
902792        2001-4-30        0
902792        2001-5-31        0
902792        2001-6-29        0
902792        2001-7-31        0
902792        2001-8-31        0
902792        2001-9-28        0
902792        2001-10-31        0
902792        2001-11-30        0
902792        2001-12-31        0
902792        2002-1-31        0
902792        2002-2-28        0
902792        2002-3-29        0
902792        2002-4-30        0
902792        2002-5-31        0
902792        2002-6-28        0
902792        2002-7-31        0
902792        2002-8-30        0
902792        2002-9-30        0
902792        2002-10-31        0
902792        2002-11-29        0
902792        2002-12-31        0
902792        2003-1-31        0
902792        2003-2-28        0

16
jingju11 发表于 2009-8-18 21:12:01
13# funwin

sorry, that is my fault.
我假设你的return 类似于以前的company。
*读入excel数据;
PROC IMPORT OUT= WORK.tab
            DATAFILE= "C:\YourBook.xls" *your excel table;
            DBMS=EXCEL REPLACE;
     RANGE="Sheet1$";
     GETNAMES=YES;*第一行留作sas变量名;
     MIXED=NO;
     SCANTEXT=YES;
     USEDATE=YES;
     SCANTIME=YES;
RUN;

data tab_;
set tab;
array dt{*} _:;*因为读入的变量以在excel中以数字开头,sas缺省性地改变第一个数字为‘_', 这也是为什么可以方便地用'_:'(以'_'开头的所有变量),也就是说在此数据里如果一些无关的变量也以’_'开头,就会有错误;
do i=1 to dim(Dt);
  company=return;*company只是名字,随便叫;
  date_=input(substrn(vname(Dt{i}),3), yymmdd10.);*应该从第三个字母截起,也是我上面的错误;
  var=dt{i};
  output;
end;
format date_ date9.;
keep company date_ var;*只保留你感兴趣的;
run;

*since your excel tables are similar, it should be better to use a macro rather than repeatedly use similar data steps and import procedures,to tackle this problem;
on the other hand, writing that one by one is readable and doable;

17
jingju11 发表于 2009-8-18 21:13:35
14# funwin

hehe, you are good; you already caught my mistake.

18
funwin 发表于 2009-8-18 21:54:37
终于搞定了 非常感谢jungju11.
能否详细 解释一下 为什么 从第三个字母截起 和 从第二个截起 有这么不同呢?
再次感谢!!!

19
jingju11 发表于 2009-8-18 22:13:08
for example, in Excel 2000-1-31-->in SAS _001_1_31-->after substrn function from postion 2-->001_1_31, SAS takes (assumes) it as 01_1_31, which can be read by SAS informat yymmdd10.;
but if in Excel 1991-1-31-->in SAS _991_1_31-->after substrn function from 2: 991_1_31, which can't be read by SAS informat yymmdd10.;
Very clearly, when we read it from position 3, that is 91_1_31, which is recognizable by SAS informat.

I think that is the reason the previous program works on 2000 and above but not on < 2000, while using substr from position 3 should have no ambiguity any more.

20
yongyitian 发表于 2009-8-18 22:56:32
楼主的问题很好。有一定的复杂性和普遍性. 楼主为何不多贴几组数据,几十小文件zip到一起,也就几个M。上传下载应该没有问题。这样可以避免产生误解。可能会有更多的人感兴趣参与讨论,编程。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 00:56