楼主: 蓝色
6306 10

[原创博文] 如何在sas中导入下列格式的数据 [推广有奖]

贵宾

已卖:4067份资源

泰斗

34%

还不是VIP/贵宾

-

TA的文库  其他...

统计软件和图书资源

Stata FAQ and Econometrics

威望
13
论坛币
1100216 个
通用积分
78894.6218
学术水平
3454 点
热心指数
3913 点
信用等级
2749 点
经验
472847 点
帖子
11699
精华
5
在线时间
20307 小时
注册时间
2004-7-15
最后登录
2025-12-28

初级热心勋章 初级信用勋章 初级学术勋章 中级学术勋章 中级热心勋章 中级信用勋章 高级热心勋章 高级信用勋章

楼主
蓝色 发表于 2011-3-9 11:06:05 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
假如原始数据如下:
2000  1  row2 col3
1   2   3
4   5   6
2000  2  row2 col3
7   8   9
10  11  12
2001  1  row2 col3
1   2   3
4   5   6
2001  2  row2 col3
7   8   9
10  11  12

现在希望用sas程序导入为如下格式:

year  id   row x1  x2  x3
2000   1    1   1     2   3
2000   1    2   4     5   6
2000   2    1   7     8   9
2000   2    2  10  11  12
2001   1    1   1     2   3
2001   1    2   4     5   6
2001   2    1   7     8   9
2001   2    2  10  11  12

该怎么写程序。
谢谢!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:sas程序 原始数据 year ear 如何 程序

回帖推荐

webgu 发表于2楼  查看完整内容

用input控制就可以。
已有 1 人评分学术水平 收起 理由
论坛数据分析 + 1 stata版主现在用SAS了啊~

总评分: 学术水平 + 1   查看全部评分

本帖被以下文库推荐

沙发
webgu 发表于 2011-3-9 12:44:47
用input控制就可以。
  1. data testds;
  2.    input year id;
  3.    do row=1 to 2;
  4.     input x1 x2 x3;
  5.     output;
  6.    end;
  7. datalines;
  8. 2000  1  row2 col3
  9. 1   2   3
  10. 4   5   6
  11. 2000  2  row2 col3
  12. 7   8   9
  13. 10  11  12
  14. 2001  1  row2 col3
  15. 1   2   3
  16. 4   5   6
  17. 2001  2  row2 col3
  18. 7   8   9
  19. 10  11  12
  20. ;
  21. proc print;
  22. run;
复制代码
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

藤椅
论坛数据分析 发表于 2011-3-9 14:26:14
我感觉您需要的应该是大样本数据的导入和结构调整

如果是excel或者txt文件的话,可以上传部分样本,这样用data +infile直接导入,用datalines对于小样本好用,大样本就不行了
老夫聊发少年狂

板凳
蓝色 发表于 2011-3-9 15:44:10
3# 论坛数据分析

给别人帮忙问的

你说的没有错是大数据集。

hadcrut3.zip
下载链接: https://bbs.pinggu.org/a-862293.html

7.43 MB

本附件包括:

  • hadcrut3.dat

报纸
论坛数据分析 发表于 2011-3-9 16:18:21
这个数据是stata格式的吗?打不开呀
老夫聊发少年狂

地板
蓝色 发表于 2011-3-9 16:57:34
论坛数据分析 发表于 2011-3-9 16:18
这个数据是stata格式的吗?打不开呀
文本格式的
写字板可以打开

7
myc_sas 发表于 2011-3-9 17:58:43
try this, it works at least, don't know if speed is ok for large data sets.

filename myfile 'D:\test.txt';
data test(drop=temp col);
        infile myfile;
        retain year id;
        input year id temp $ @;
        if index(temp, 'row')>0 then do;
                input col $ @;
                do row=1 to 2;
                        input x1 x2 x3;
                        output;
                end;
        end;
run;

8
ntsean 发表于 2011-3-9 21:47:23
试了下你的 data,发现都是 row=36, column=72
所以我就直接用36和72写程序了,能读入你的数据,不过程序不够general
如果要想更加通用,可以读入 nrow, ncolumn, 控制读入row,column的size,不过这个改一下也不难
还有一个,就是 missing value, 因为每个的 missing value的label都一样是:1E-30,所以也很容易用data step控制,把这些值设成 missing .

程序如下,数据不大,几秒钟就读完了

data a;
infile 'c:\hadcrut3.dat' LRECL=1000;
input year id;
row=0;
do i=1 to 36;
        row+1;
        input x1-x72;
        output;
end;
run;
已有 1 人评分论坛币 收起 理由
论坛数据分析 + 999 精彩帖子

总评分: 论坛币 + 999   查看全部评分

9
myc_sas 发表于 2011-3-9 21:50:23
filename myfile 'D:\hadcrut3.dat';
data test2(drop=nouse maxrow rows maxcol cols missing);
        infile myfile lrecl=32767;
        retain year id;
        input year id nouse maxrow rows $ maxcol cols $ missing $18. @;
        if rows='rows' then do;
                missing=input(substr(missing, 9), 15.);
                do row=1 to 36;
                        input x1-x72;
                        output;
                end;
        end;
run;

10
myc_sas 发表于 2011-3-9 23:05:06
my final version including:
1) read numbers of rows/columns for each data block (dynamic instead of hard coded input of 36/72)
2) read/change for missing values for each data block


filename myfile 'D:\hadcrut3.dat';

data _null_;
        infile myfile end=last;
        input year $ @;
        if length(year)=4 then do;
                block+1;
                input id nouse maxrow rows $ maxcol cols $ missing $18. ;
                call symput(compress(left(trim('missing_value'||put(block,4.)))),
                                input(substr(missing, 9), 15.));
                call symput(compress(left(trim('maxrow'||put(block,4.)))), maxrow);
                call symput(compress(left(trim('maxcol'||put(block,4.)))), maxcol);
        end;
        if last then do;
                call symput('countblock', put(block,4.));
        end;
run;
%macro read;
data test2(drop=nouse maxrow rows maxcol cols missing);
        infile myfile lrecl=32767;
        %do i=1 %to &countblock;
                %let x_maxcol=&&maxcol&i;
                retain year id;
                input year id nouse maxrow rows $ maxcol cols $ missing $18. @;
                if rows='rows' then do;
                        do row=1 to maxrow;
                                input x1-x&x_maxcol;
                               
                                %do j=1 %to &x_maxcol;
                                        if x&j =&&missing_value&i then x&j=.;
                                %end;
                               
                                output;
                        end;
                end;
        %end;
run;
%mend read;
%read

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 11:30