1.數(shù)據(jù)合并
(1)利用set語句縱向合并
data?數(shù)據(jù)集;
? ? ?set?數(shù)據(jù)集1(數(shù)據(jù)集選項)數(shù)據(jù)集2(數(shù)據(jù)集選項).....;
run;
說明:將若干個數(shù)據(jù)集縱向合并,并存放在data語句建立的數(shù)據(jù)集中;當set后面只有一個數(shù)據(jù)集時,就相當于復制的作用。當要合并的變量在不同數(shù)據(jù)集中變量類型不一致時,無法直接合并,需轉(zhuǎn)換變量類型。
注意:data語句的作用是創(chuàng)建數(shù)據(jù)集,不是打開數(shù)據(jù)集。
兩個非常實用的數(shù)據(jù)集選項:
set?數(shù)據(jù)集1(in=臨時變量1)數(shù)據(jù)集2(in=臨時變量2).....;?
*當合并的記錄屬于數(shù)據(jù)集1,“臨時變量1”的值為1,否則為0;臨時變量不會在結(jié)果出顯示,需賦值給新變量,即“新變量=臨時變量”;
set?數(shù)據(jù)集1(rename=(原名1=新名1....)數(shù)據(jù)集2(rename=(原名2=新名2....)).....; *當變量在不同數(shù)據(jù)集中名不一樣,則需重命名;
(2)利用Merge語句橫向合并:
data?數(shù)據(jù)集;
? ? ?merge?數(shù)據(jù)集1(數(shù)據(jù)集選項1) 數(shù)據(jù)集2(數(shù)據(jù)集選項2)....;
? ? ?by?變量1?變量2 .....;
run;
說明:by語句指定索引(如:id); 如果數(shù)據(jù)集事先沒有按索引變量(id)排序,?需先分別對各個數(shù)據(jù)集排序后才能合并。
*==合并ab1和ab2數(shù)據(jù)集的交集;
data? ab;
? ? ? ? merge ab1(in=d1) ab2(in=d2); /*產(chǎn)生標識兩個數(shù)據(jù)集的臨時變量ab1和ab2*/
? ? ? ? by id;
? ? ? ? if d1=1 and d2=1;
proc print;
run;
2.數(shù)據(jù)對比
proc compare <base=數(shù)據(jù)集1 compare=數(shù)據(jù)集2> <nosummary>? <transpose>;
? ? ? ? ?by?變量1?變量2 .......;
? ? ? ? ?id??變量1 變量2 .......;
run;
說明:選項base和compare分別指定兩個比較和被比較的數(shù)據(jù)集; “nosummary”:不顯示一些概括性的結(jié)果;“transpose”按記錄顯y示不一致的結(jié)果,如不指定該選項,默認按變量顯示不一致的結(jié)果。by語句:指定索引變量,避免錯位;id語句:指定索引變量,可以讓你方便地根據(jù)變量找到相應(yīng)的觀測,如不指定,默認結(jié)果只顯示第幾行;
proc compare base=ab compare=cd nosummsry transpose;
? ? ? by id;
? ? ??by id;
run;
3.查找異常值
一般式:
data 新數(shù)據(jù)集;
????????set 已有數(shù)據(jù)集;
????????if|where 條件語句;
proc print;
run;
示例:
data outline;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data outline;
? ? ?set?dem.b1;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??set dem.b1;
? ? ?if (gender not in(1,2));? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?where (gender not in(1,2));
proc print;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??proc print;
run;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?run;
IF和where的區(qū)別:
? ?1.IF和where都可以應(yīng)用:
? ? ? set?有條件的復制數(shù)據(jù)集時,其后面緊跟的條件語句可以用IF或where。?但where運行更快,因where是在數(shù)據(jù)讀入前就執(zhí)行選擇條件的,IF是在讀入數(shù)據(jù)后執(zhí)行。
2.只能用IF語句的情況:(只能用IF語句,不能用where)
? ? ?(1)使用SAS自動變量時;
? ? ?(2)當指定的條件變量是新產(chǎn)生的變量時;
3.只能用where語句的情況:(只能用where,不能用IF語句)
? ? ?(1)當使用了某些特殊運算符時;
? ? ?(2)當調(diào)用proc過程,只選擇部分觀測執(zhí)行該過程時;
? ? ?(3)當做數(shù)據(jù)集選項時,只能用where。
特殊運算符作用
between......and指定介于數(shù)值變量兩個值之間的觀測
contains "指定的字符”指定字符變量中包含指定字符的規(guī)則
like?"指定的字符”指定字符變量中與指定字符相似的規(guī)則,
模糊部分可用%或_代替,其中%代表多個字符,_代表1個字符。
is null或 is missing指定包含缺失值的觀測
like "_t_"?指查找變量長度為3且中間為“t”的觀測;like "%t%"?查找任意含t的觀測。
4.缺失值的填補
SAS主要通過proc mi過程來實現(xiàn),mi是mutiple impuation(多重填補)的縮寫。
proc mi <out=數(shù)據(jù)集> <round=> <mininum=> <maximum> ;
? ? ?mcmc;
? ? ?var?變量1?變量2.......;
run;
說明:“out=數(shù)據(jù)集”是把填補后的數(shù)據(jù)保存到自定義的數(shù)據(jù)集中;“round=”選項是指定填補值的小數(shù)位數(shù),round=1表示保留只整數(shù);“mininum”和“maximum”是指定填補值的最小值和最大值;
? ? ? ? ? “mcmc”語句是采用馬爾科夫鏈蒙特卡羅模擬方法來產(chǎn)生一個抽樣分布,作為填補缺失值的填補技術(shù); “var”指定哪些變量需要填補。
? ? ? ? ? ? mcmc, Mrakov chain Monet Carlo.
? ? ? ? ? ?SAS?默認產(chǎn)生5個填補完整的數(shù)據(jù)集,每個數(shù)據(jù)集的填補值都不同。可用proc mianalyze過程,對5次填補結(jié)果進行綜合分析。
示例:
data b1_imputed;
? ? ?set dem.b1;
run;
proc mi data=b1_imputed out=nomissing round=1 1 1 mininum=150 1 1 maximum=200 5 5;
? ? ?mcmc;
? ? ?var heigh y2 y4;
run;
proc print data=nomissing;
run;
缺失數(shù)據(jù)的更新
data 新數(shù)據(jù)集;
? ? ?update?舊數(shù)據(jù)集?新數(shù)據(jù)集;
? ? ? ? ?by?索引變量;
run;