SAS中文论坛

标题: <一個處理資料的問題...請教高手> [打印本页]

作者: shiyiming    时间: 2009-3-2 18:53
标题: <一個處理資料的問題...請教高手>
我有兩個問題:

1.我有一筆資料是這樣的
變數 sic1 sic2 sci3 sci4 sci5 sic6 sic7 sic8 sci9 sic10
資料   12    .     12    .     34    .     .      .     .      .     
         23    23    .     .      .      .    .      .      .      .     
         33    45    23   .      .      .    .      34    .      .
我想要找出sic1-sic10他們的值是否相同,要是全相等則dummy=1,重點是我要忽略missing值然後檢測,

像是第一行的dummy應該是0,而第二行應該dummy=1,以此類推~

2.我有一筆資料是:
sic   value
01      234
02      634
03      423
04      234

然後我想將上述資料填入以下資料

假設:
sic1 sic2 sci3 sci4 sci5 sic6 sic7 sic8 sci9 sic10
01     02    .     .     .       .     .     .      .      .
.       02    03   04   .      .     .      .      .      .

結果應該要變成:

sic1 sic2 sci3  sci4   sci5 sic6 sic7 sic8 sci9 sic10
234  634    .      .      .       .     .     .      .      .
.      634   423  234   .       .     .     .      .      .


希望有人好心可以幫我解答~謝謝~
作者: shiyiming    时间: 2009-3-2 22:23
标题: Re: <一個處理資料的問題...請教高手>
第一个是不是看那几个变量不为缺失值再进行处理?用missing搞定。
第二个可以用format,将01换成格式234,依次类推。
作者: shiyiming    时间: 2009-3-3 09:34
标题: Re: <一個處理資料的問題...請教高手>
我不大懂你的意思~
1.第一個有missing值所以不知道怎麼寫if指令
if sic1=sic2=sci3=sic4=sci5=sic6=sic7=sic8=sic9=sic10 then dummy=1;
可以這樣寫的話就會把missing值考慮進去
有沒有什摸辦法讓他不考慮missing值

2.format的意思是怎麼用
因為這不是讀資料而是要把資料比對進去
我想做的是如果sic=01那他就會自動去下面的答案找01=234
01  234
02  453
03  456
04   234
作者: shiyiming    时间: 2009-3-3 21:04
标题: Re: <一個處理資料的問題...請教高手>
1.就是if not missing(sic1)。。。and not missing(sic10)then do;
         if sic1=sic2=sci3=sic4=sci5=sic6=sic7=sic8=sic9=sic10 then dummy=1;
2.proc format ;
values $ tran '01'=234
                 '02'=453
                 '03'=456
                 '04'=234
                other='other'
               ;
run;
然后a=put(sic,$tran.)转换下旧可以了。
作者: shiyiming    时间: 2009-3-4 00:28
标题: Re: <一個處理資料的問題...請教高手>
不˙好意思~
可以把程式碼列出來給我看嗎~

因為實在試不出來~
拜託~
作者: shiyiming    时间: 2009-3-4 11:51
标题: Re: <一個處理資料的問題...請教高手>
你的意思是這樣嗎:
           data all1;
           set all;
if sic1^= . then do;
if sic2^= . then do;
if sic3^= . then do;
if sic4^= . then do;
if sic5^= . then do;
if sic6^= . then do;
if sic7^= . then do;
if sic8^= . then do;
if sic9^= . then do;
if sic10^= . then do;
           if sic1=sic2=SIC3= sCI4=siC5=sic6=sic7=sic8=sic9=sic10 then dummy=1;

end;
end;
end;
end;
end;
end;
end;
end;
end;
end;


           run;
作者: Qiong    时间: 2009-3-10 14:48
标题: Re: <一個處理資料的問題...請教高手>
Q1
[code:19s4zsrt]
data temp;
set temp;
array s(*)_all_;
yummy=1;
do i=2 to dim(s);
if s(i)^=&#46; and s(i)^=s(1) then yummy=0;
end;
drop i;
run;[/code:19s4zsrt]
作者: Qiong    时间: 2009-3-10 15:09
标题: Re: <一個處理資料的問題...請教高手>
方便起见,把对应的数据复制进去就行,code不用改。
[code:24e99oh7]
data format;
input sic $2&#46;  value $ ;
cards;
01 234
02 634
03 423
04 234
;
data format;
  set format;
  informat start $16&#46; end $16&#46; label $40&#46; fmtname $32&#46; type $1&#46;;
  start=sic;
  end=sic;
  label=value;
  fmtname='aa';
  type='c';
  keep start end label fmtname type;
run;
proc format library=work cntlin=format;run;
data new;
informat sic1-sic10  $2&#46; ;
format sic1-sic10 $aa&#46;;
input sic1-sic10  $ ;
cards;
01 02 &#46; &#46; &#46; &#46; &#46; &#46; &#46; &#46;
&#46; 02 03 04 &#46; &#46; &#46; &#46; &#46; &#46;
;
run;[/code:24e99oh7]
作者: shiyiming    时间: 2009-3-12 21:24
标题: Re: <一個處理資料的問題...請教高手>
謝謝你的答覆~第一題我寫出來了

但是第二題我還是看不董
proc format library=work cntlin=format;run;~~~~~~這是要幹麻??可以稍微解釋一下嗎~
data new;
informat sic1-sic10  $2. ;
format sic1-sic10 $aa.;
input sic1-sic10  $ ;
cards;
01 02 . . . . . . . .
. 02 03 04 . . . . . .
;
run;
作者: Qiong    时间: 2009-3-13 09:29
标题: Re: <一個處理資料的問題...請教高手>
利用一个dataset来定义format,dataset要有数据的上下限,对应取值,格式名称,类型。
这样对于要定义的format分组比较多的时候,比较方便,我不知道你的分组是不是很多,就用了这个方法。
对于分组比较少的,前面的其实更方便。
[code:3j186w5k]proc format ;
values $ tran '01'=234
'02'=453
'03'=456
'04'=234
other='other'
;[/code:3j186w5k]
作者: Qiong    时间: 2009-3-13 09:41
标题: Re: <一個處理資料的問題...請教高手>
[quote:1bnck0on]
2.format的意思是怎麼用
因為這不是讀資料而是要把資料比對進去
我想做的是如果sic=01那他就會自動去下面的答案找01=234
[/quote:1bnck0on]
怎么个自动法??
作者: shiyiming    时间: 2009-3-13 11:59
标题: Re: <一個處理資料的問題...請教高手>
首先~真的很感謝妳幫我解答:
我的意思是假設我已經知道一比資料如下:
sic   value
01   234
02   634
03   423
04   234
.        .
.        .
.        .
99    789

然後假設:
sic1   sic2   sci3   sci4   sci5   sic6   sic7   sic8   sci9   sic10
01     02     
  .      02      03    04       .        .        .         .      .        .

結果應該要變成:
所以說我想利用我已知的資料填入

sic1   sic2     sci3    sci4   sci5   sic6   sic7   sic8   sci9   sic10
234     634       .       .        .        .        .        .       .        .     
  .       634     423    234     .        .        .         .      .        .

其時我的資料還蠻多的,大概有好幾萬筆要填,所以想說有沒有什麼程式碼可以直接幫我讀取寫入。
作者: Qiong    时间: 2009-3-13 16:50
标题: Re: <一個處理資料的問題...請教高手>
还是没有很明白……
把2组资料都放在txt,然后生成一个txt,放第三组数据?
够自动么?
作者: Qiong    时间: 2009-3-13 17:09
标题: Re: <一個處理資料的問題...請教高手>
好像没有帮到忙的样子,你的原始数据是什么格式的?txt还是sas的dataset阿。
如果是dataset,直接用format就好啦。
如果是txt, 改改前四行的宏变量,直接跑程序就好啦。
file1是放那几万行的文件名,file2是放你要改的文件名。
[code:1hsa2ko3] %let savepath=%str(D&#58;\temp\test);
%let file1=%str(temp1);
%let file2=%str(temp2);
%let outfile=%str(temp3);

Data format;
infile &quot;&amp;savepath\&amp;file1&#46;&#46;txt&quot; firstobs=1;
input sic $2&#46;  value $ ;
;
data format;
  set format;
  informat start $16&#46; end $16&#46; label $40&#46; fmtname $32&#46; type $1&#46;;
  start=sic;
  end=sic;
  label=value;
  fmtname='aa';
  type='c';
  keep start end label fmtname type;
run;
proc format library=work cntlin=format;run;

data new;
infile &quot;&amp;savepath\&amp;file2&#46;&#46;txt&quot;    firstobs=1;
informat sic1-sic10  $2&#46; ;
format sic1-sic10 $aa&#46;;
input sic1-sic10  $ ;
run;
PROC EXPORT DATA= new   OUTFILE= &quot;&amp;savepath\&amp;outfile&#46;&#46;txt&quot;   
            DBMS=TAB REPLACE;
RUN;[/code:1hsa2ko3]
作者: Qiong    时间: 2009-3-13 17:13
标题: Re: <一個處理資料的問題...請教高手>
temp1.txt
---------------------------
01 234
02 634
03 423
04 234
99 789
=========================
temp2.txt
---------------------------
01 02 . . . . . . . .
. 02 03 04 . . . . . .
=========================
temp3.txt
---------------------------
sic1        sic2        sic3        sic4        sic5        sic6        sic7        sic8        sic9        sic10
234        634                                                               
        634        423        234




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