SAS中文论坛

标题: 多维数据导入与排列 [打印本页]

作者: shiyiming    时间: 2007-5-30 10:21
标题: 多维数据导入与排列
问题如下:
需要将一个多维数据(年份、地区,以及不同年份经济发展指标)(见附件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 -->
作者: shiyiming    时间: 2007-7-17 00:21
标题: 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
作者: shiyiming    时间: 2007-9-6 15:23
标题: 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)




欢迎光临 SAS中文论坛 (https://mysas.net/forum/) Powered by Discuz! X3.2