sas數據操作-by、merge


By語句

By語句用於規定分組變量,控制set,merge,update或modify語句

官方說明:

BY<DESCENDING> variable-1<...<DESCENDING>variable-n> <NOTSORTED> ; 

specifies the variable that the procedure uses to form BY groups. You can specify more than one variable. By default, the procedure expects observations in the data set to be sorted in ascending order by all the variables that you specify or to be indexed appropriately.

簡單意思為:默認為升序,可以聲明多個變量

聲明為降序:by descending var1 descending var2;每個變量前面都要寫上descending.

NOTSORTED:specifies that observations with the same BY value are grouped together, but are not necessarily sorted in alphabetical or numeric order. The observations can be grouped in another way (for example, in chronological order).

聲明所有by組的觀測在一起,但是沒有被排序,如果前面用到了descending,則會覆蓋掉,使其無用

 

data me;
  set sashelp.class;
run;
/*排序后me的內容會發生變化*/
proc
sort data=work.me; by sex age; run;
/*排序后的變量才能使用by,沒排序的變量如果放在排序變量前會出現錯誤,放在后面沒事兒,這里是產生first和last觀測值的步驟*/
/*意義在於尋找by組內的第一個和最后一個觀測值*/ data fst_lst;
set me; by sex age ; fisrt_a = first.age; last_a = last.age; fisrt_n = first.sex; last_n = last.sex; run;

/*復制到sas中會飄紅,是格式問題,刪除前面的空格即可*/
data fst_data last_data;
  set me;
  by sex age;
  if first.sex then output fst_data;
  if last.sex then output lst_data;
run;

 

2:Merge語句

Merge有四種形式,在連接時需要注意的幾個問題是

1:how each method treats duplicate values of common variables

2:how each method treats missing values or nonmatched values of common variables

注意的小問題

如果有同名變量,那么后一個數據集的變量會覆蓋前一個數據集中的同名變量

The MERGE statement returns both matches and non-matches by default

Merge的變量需要相同的type和name,但是不需要相同的length。

如果沒有相同的type,error和warning會寫到log上,並且merge失敗

Merge前必須對by中的變量進行排序操作,或by變量有是索引列

無論是哪種Merge,如果沒有by,都會一輪轉化為Unmatched Merge

 

 

 

2.1:One to One Unmatched Merge

SAS simply joins together the first observation from each data set, then the second observation from each data set, and so on

就是一一對應的進行合並,按位置來,不按by中的變量來 

sas內部的運作形式

1:在描述文件中讀取描述變量的信息,並和新創建的變量一起建立pdv,並賦為缺失值。

2:按merge中數據集的順序讀取觀測值,如果有同樣的變量,則后面的變量覆蓋掉前面的變量。當執行到run前面一句時,將pdv中的數據寫入數據集,但是只會將新創建的變量賦值為缺失

3:一直進行,知道所有觀測值讀入完畢

 

 

Match Merge

Match-merging combines observations from two or more SAS data sets into a single observation in a new data set according to the values of a common variable

The number of observations in the new data set is the sum of the largest number of observations in each BY group in all data sets 

sas內部的運作形式

1:在描述文件中讀取描述變量的信息,並和新創建的變量一起建立pdv,並賦為缺失值,同時建立first.variable 、last.variable變量

2:sas比較每個數據集中的第一個by group來決定在新數據集中哪個應該先出現,sas將兩個數據集中第一個出現的數據合並,並寫入pdv。如果另一個數據集沒有相應數據,則寫入缺失值。

3:sas將pdv中的數據寫入新數據集,保留pdv的數據,將新創建的變量置為缺失。繼續進行連接,直到第一個by group中的變量全部連接完畢。然后重復上述過程直到所有合並by group完畢。

 

 

2.2:One to One Match Merge

besides the BY variables,variables from the second data set will overwrite any variables having the same name in the first data set.  

 

2.3:One-to-Many Match Merge

只對對應的by組變量進行連接

新數據集中的觀測數量是所有by group中最大數量的和。

對於一對多的merge 用一個和多個進行一一連接。

 

2.3:Many-to-Many Match Merge

一一對應完成后,剩下的跟同組的最后一個殘留值對應,也是采取一一對應的策略,只要記牢一點,merge和set不對應的情況下情況pdv中數據集的原有值 

 

 2.4:對於數據不對應的情況

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM