SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 1288|回复: 2
打印 上一主题 下一主题

多维数据导入与排列

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2007-5-30 10:21:46 | 只看该作者

多维数据导入与排列

问题如下:
需要将一个多维数据(年份、地区,以及不同年份经济发展指标)(见附件excel表),如:
2003year 2004year 2005year
region gdp load gdp load gdp load
sh 200 150 300 200 400 300
sz 180 160 220 180 300 260
nj 120 100 160 130 220 190
...

要求:按地区(region)重新生成多个数据表,如:
数据表1 sh
year gdp load
2003 200 150
2004 300 200
2005 400 300 ,
数据表2 sz
year gdp load
2003 200 150
2004 300 200
2005 400 300 ,
...
一直到最后一个数据表。

要单独生成,因为数据表生成后还需对每一个数据表进行回归分析。
ps:在将excel数据用proc import导入sas时第二行变量名(gdp,load)不显示,不知道为什么?

谢谢各位高手指点:)

不能发附件呀,附件参见:
<!-- m --><a class="postlink" href="http://www.sasor.feoh.net/modules.php?name=Forums&amp;file=viewtopic&amp;t=3264&amp;sid=ef42fd33d0f730f8715ae8437dc6b5d5">http://www.sasor.feoh.net/modules.php?n ... 437dc6b5d5</a><!-- m -->
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2007-7-17 00:21:57 | 只看该作者

Re: 多维数据导入与排列

假设导入的数据集为temp1,生成的数据集已region的名命名:
proc sql;
select distinct region into: tempvar from temp1;
select num(distinct region) into:tempnum from temp1;
%macro temp;
data &amp;tempvar;
set temp1;
%do i =1 %to &amp;tempnum.;
if region=&quot;%scan(&amp;tempvar.,&amp;i.)&quot; then output %scan(&amp;tempvar.,&amp;i.);
%end;
run;
%mend temp;
%temp
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2007-9-6 15:23:06 | 只看该作者

Re: 多维数据导入与排列

谢谢楼上回复

我也想了一个办法

%let dir=E:\temp1\;
LIBNAME excellib excel &quot;e:\temp1\data.xls&quot; ;

%macro ReadXls (in=,out=);
/*libname excellib excel &quot;&amp;dir.\&amp;inf&quot;; /* STEP 1 */

proc sql noprint; /* STEP 2 */
create table sheetname as
select tranwrd(memname, &quot;''&quot;, &quot;'&quot;) as sheetname
from sashelp.vstabvw
where libname=&quot;EXCELLIB&quot;;
select count(DISTINCT sheetname) into:cnt_sht
from sheetname;
select DISTINCT sheetname into :sheet1 - :sheet%left(&amp;cnt_sht)
from sheetname;
quit;

libname excellib clear; /* STEP 3 */

%do i=1 %to &amp;cnt_sht;
proc import datafile=&quot;&amp;dir.\&amp;in&quot; /* STEP 4 */
out=_sheet dbms = excel2000 replace;
sheet=&quot;&amp;&amp;sheet&amp;i&quot;;
getnames=yes;
length region $40;
run;

data temp2;
set _sheet;
from =&quot;&amp;&amp;sheet&amp;i&quot;;
year = substr(from,2,4);
drop from;
run;

/* APPEND DATA FROM EACH EXCEL TO A MAIN TABLE */
%if &amp;i = 1 %then %do;
data &amp;out;
set temp2;
run;
%end;
%else %do;
data &amp;out;
set &amp;out temp2;
run;
%end;
%end;
/*proc append base=master
data=sheet&amp;i force; /* STEP 5 */
/*run;%end;*/
%mend ReadXls;
%ReadXls (in=data.xls,out=fromexcel)
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2026-2-4 07:13 , Processed in 0.068103 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表