sas數據讀取詳解:SAS讀取小工具,讀取數值類型的注意事項,lrecl選項,四種讀取數據方式以及數據指針的位置、讀取mess data的、infile語句及其選項(dsd dlm missover truncover obs firstobs)、proc import、自定義缺失值


SAS讀取小工具

  1:Column-Pointer Controls

    @n : move the pointer to the n column in the input buffer.

     +n : move the pointer forward n columns in the input buffer.

     /    : moves the pointer to the next line in the input buffer.

    #n  : moves the pointer to the nth line in the input buffer.

  2:Line-Hold Specifiers

             @  : prevent SAS from automatically reading a new data record into the input buffer when a new input statement is executed within the same iteration of the DATA step.When used,the trailing @ must be the last item  in the INPUT statement.

    @@ : prevent SAS from automatically reading a new data record into the input buffer when the next INPUT statement is executed,even if the DATA steps return to the top of for another iteration.When used, the double trailing @ must be the last item in the INPUT statement.

      3:

      4:data values

  1-->numeric values, store in the float-point format.

  2-->character value, a sequence of characters.

tricks: 0023的數據會被當做23讀入, 讀取數據不會儲存空白區域。

     想要將讀入的字符全部大寫顯示,可以使用CAPS system option or the $UPCASE informat.

                         OPTIONS CAPS;         FORMAT VARNAME $UPCASE.;

(The record length is the number of characters, including spaces, in a data line.) If your data lines are long, and it looks like SAS is not reading all your data, then use the LRECL= option in the INFILE statement to specify a record length at least as long as the longest record in your data file.  

filename test 'E:\sasData\num1.txt';

data res;
    infile test lrecl=256;
    label name='姓名' age='年齡';
    input name$ age;
run;

1:三種讀取數據的方式

1.1:list input 

  List input uses a scanning method for locating data values,data are not required to be aligned in columns but must be separated by at least blank or

by the DLMspecified with the DLM= or DLMSTR= in the file statement. if you want SAS read consecutive delimiter as if there is a missing value between them,

specify the DSD option in the infile statement.

變量名字符數小於32,字符型變量后加$, 兩個變量間要有間隔。缺失值用 . 表示。

INPUT Name $ Age Height; 可行 盡量用這種,美觀,易閱讀

INPUT Name $Age Height; 可行

INPUT Name$ Age Height;可行 

INPUT Name$Age Height; 可行

 

適用范圍?

對於日期型或其他復雜類型不適用

適用於字符(字符中不能帶有空格)和數字,並以空格為分隔符的數據

 

列指針停留在哪?

data two;
    input x $ y 1.;
datalines;
10 1  /*讀入結果10 1*/
10  1 /**讀入結果10 ./
100000000011 2 /*讀入結果10000000 2*/ ; run;
/*表明list input的指針停留在最后一個讀入參數的緊挨着的空格的下一個位置*/

 

1.2:Column input 

適用范圍?

對於對應變量值都在固定列的數據十分有用

 

優點?

讀入的字符變量值可以帶有空格

可以自由的選擇讀入的列,不必按順序讀入

縮減文件的存儲空間,如果有空格分隔,那么空間將會增加一倍

對於缺失值的話讀入空白列即可

 

列指針指針停留在哪?

停留在指定的列的下一列

INPUT Name $ 1-10 Age 11-13 Height 14-18; 

 

以上兩種都只能讀入標准數值類型,比如Standard numeric data contain only numerals, decimal points, minus signs, and E for scientific notation.

無法讀入Numbers with embedded commas or dollar signs are examples這些非標准的數值類型。 例如100,100,1  $100  日期型這些數據

 

1.3:格式輸入

w=total width    d=number of decimal

字符格式 $informatw.                  w.是標准的字符讀入格式(不帶informat)

數值格式 informatw.d                  w.d是標准的數值讀入格式(不帶informat)

日期格式 informatw.

格式輸入都是從第一列開始讀入數據,如果對應列數不對,讀入的數據也會有誤

對於混合輸入,其開始位置依據其他兩種輸入法的數據指針的位置進行讀取數據

 

列指針指針停留在哪?

停留在規定的長度的下一格

data one;
    INPUT Name $16. Age 3. +1 Type $1. +1 Date MMDDYY10.   (Score1-Score5) (4.1); 
datalines;
Alicia Grossman  13 c 10-28-2008 7.8 6.5 7.2 8.0 7.9 
;
run;

常用的可選擇的格式

讀入數據及其結果展示

 

1.4:混合輸入

取長補短,中西合並!!結合三種輸入法的優缺點進行數據讀取

data one;
    INPUT name $ 1-22 state $ year @40 size comma9.; 
datalines;
Yellowstone           ID/MT/WY 1872    4,065,493
Everglades            FL 1934          1,398,800
Yosemite              CA 1864            760,917
Great Smoky Mountains NC/TN 1926         520,269
Wolf Trap Farm        VA 1966                130
;
run;
/*重點是數據指針的位置,上面的三種讀入方式有描述*/

 

2:讀取mess data

有時候,前面的四種方式都勝任不了讀入的任務,那么需要更多的工具來完成相應的目標

The @‘character’ column pointer

sometimes you don’t know the starting column of the data, but you do know that it always comes after a particular character or word

The colon modifier :

If you only want SAS to read until it encounters a space,then you can use a colon modifier on the informat

對於字符型格式輸入,如果是默認長度,則讀取的數據長度小於等於8。

如果規定長度,比如$20. 則可能讀取到不想要的數據

這時候可以用 colon modifier --->>> :

/*要讀取的數據*/
My dog Sam Breed: Rottweiler Vet Bills: $478 /*不同的讀取方式和結果*/ INPUT @’Breed:’ DogBreed $; Rottweil INPUT @’Breed:’ DogBreed $20.; Rottweiler Vet Bill INPUT @’Breed:’ DogBreed :$20.; Rottweiler

Tilde  ~

format modifier enables you to read and retain single quatation marks,double quatation marks,and delimiters wihtin character value.

 

Specify the missing value.

Data new;
    missing a b c;
    input name $ score1-score3;
cards;
yi     1 a 2
can   b 1 3
ling   c 1 1
;
run;

 

 

3:數據讀取技巧

3.1:一個input讀取多行數據

如果input中的變量在一行數據中沒有全部讀取完,那么他會自動轉到下一行讀取數據

與其這樣,不如我們自己明確規定他應該轉到哪行去讀取

DATA highlow;
INPUT City $ State $ / NormalHigh NormalLow #3 RecordHigh RecordLow; */表示讀取下一行 #n表示讀取第n行,這里的第n行應理解為一次input循環中所相對的第n行,比如第一次88 29是第3行,第二次97 65就是是3行;
    datalines;
        Nome AK
        55 44
        88 29
        Miami FL
        90 75
        97 65
        Raleigh NC
        88 68
        105 50 
    ;
RUN; 

 

/與#n的重要區別,以及與lostcard搭配的使用

lostcard的作用:Discards the first record in the group of records being read,Attempts to build an observation by beginning with the second record in the group

使用/讀取多行數據時,例如input var1 / var2;var1先被讀入input緩沖流中,然后/表示換行將換行后的數據也讀入同一個緩沖流中,這樣就意味着,必須要先讀入var1才能讀入var2。

而使用#n時,那么就意味着系統會開創多條input緩沖流。這樣我們就能讀var2再讀var1;input #2var2 #1var1;

*#n與lostcard結合的實例;
data res;
input id test1 #2 idcheck test2 test3; *使用#2時會得到正確的輸出結果301 304,但是使用/時只能得到一條輸出,因為使用/時第三行和第四行拼湊成了一條緩沖流,下次input會直接輸入第五行而不會從第四行輸入;
    if id ne idcheck then do; 
    put id= idcheck=; lostcard;
    end;
    cards;
301 92
301 61 75
303 92 78
304 85
304 90 89
    ;
run;

proc print noobs;run;

 

3.2:將一行數據讀取到多個觀測值

雙尾@@  ,當使用時告訴sas不要換行,除非數據讀取完畢,或者遇到一個不以@@結尾的input,當input遇到一行結尾而還有變量沒讀取時會自動切換到下一行

This line-hold specifier is like a stop sign telling SAS, “Stop, hold that line of raw data.” 

data body_fat;
   input Gender $ PercentFat @@;
   datalines; 
m 13.3 f     
m 22   f 
23.2    
m 16   m 12    
;

 

3.3:按條件讀入需要的數據,分段讀取一條數據

單尾@

record理解為一橫行,@的主要功能是可以在一個循環中分段讀取一個record,來達到是否需要當前觀測值的效果

data red_team;
   input Team $ 13-18 @;  1 
   if Team='red';  2 
   input IdNumber 1-4 StartWeight 20-22 EndWeight 24-26;  3 
   datalines;
David  red    189 165
Amelia yellow 145 124
Alan   red    210 192
Ravi   yellow 194 177
Ashley red    127 118
Jim    yellow 220   . 
;  4 

proc print data=red_team;  
   title 'Red Team';
run;

流程詳解:
1:input讀取第一條記錄進入緩沖流,從13-18讀取數據,然后賦值給pdv中的變量Team,單尾@將緩沖流中的record位置保存下來
2:if進行判斷,如果是‘red’則進行下一個input語句,否則當前data循環停止,返回data步開始,將所有pdv中的變量賦為缺失值,並釋放緩沖流中維持的record
3:如果是‘red’則進入下一個input,繼續從緩沖流當前維持的record位置讀取數據,並將值賦值給pdv中的變量IdNumber, StartWeight, and EndWeight
4:返回data步開始並釋放緩沖流,並清空pdv

 

單尾@與雙尾@的異同

相同點:

他們都是line-hold specifier

不同點:

The trailing @ holds a line of data for subsequent INPUT statements, but releases that line of data when SAS returns to the top of the DATA step to begin building the next observation

The double trailing @ holds a line of data for subsequent INPUT statements even when SAS starts building a new observation.

簡而言之是單尾抓一輪,雙尾抓死不放手!!!

 

4:infile語句以及其中的選項

sas讀取數據有很多假設,比如一個input中的變量沒讀取完畢、或者是一個變量讀了一半另一半在下一行,sas會自動跳到下一個數據行讀取數據進入input中,有時這不是我們需要的,用infile中的選項可以解決很多這種問題

firstobs=  tells SAS at what line to begin reading data

obs=       It tells SAS to stop reading when it gets to that line in the raw data file

Syntax: filename fireref 'path';

 

/****************************
Ice-cream sales data for the summer 
Flavor     Location   Boxes sold 
Chocolate  213        123 
Vanilla    213        512 
Chocolate  415        242 
Data verified by Blake White 
數據集中的數據
 ****************************/

filename test 'E:\sasData\num1.txt';

data res;
    infile test firstobs=3 obs=5;
    input x :$12. y  z;    
run;

Missover :MISSOVER option tells SAS that if it runs out of data, don’t go to the next data line.Instead, assign missing values to any remaining variables.

他告訴sas,說:"input沒讀完就別讀啦,直接全部給我賦為缺失值!!!"

truncover::option tells SAS to read data for the variable until it reaches the end of the data line, or the last column specified in the format or column range, whichever comes first

上面時truncover的結果,下面是missover

根據字面意思也可以區分,trunc是截斷,表明是有多少留多少!

miss是丟失,表明沒有那么多就干脆為缺失吧!

 

 

dlm='?'  標明你要使用的分隔符    ,對於常用的csv文件用dlm=',';對用tab分隔符如果電腦編碼是ASCII則用dlm='09'x;如果編碼為EBCDIC則用dlm='05'X;

dlmstr='?' 標明你要使用的字符串分隔符

dsd(delimiter-sensitive data)這個符號做了三件事

1:忽視引號內的分隔符

2:當讀取帶引號的字符串時,不把引號內讀作數據的一部分(比如讀取"abc",讀入數據集中的內容是abc)

3:將兩個連續的分隔符當做缺失值處理(默認','為分隔符,要改變的話要再加dlm=選項,默認情況下sas會將多個連續分隔符當成一個處理)

 

5:PROC IMPORT過程。

import過程會預先掃描20行來判斷變量對應的類型,並且會根據你的文件后綴來判斷你的分隔符,如果是.csv則會用',',.txt則用'09'X,其他的需要自己聲明。

會忽略引號,並將兩個連續分隔符當做缺失值處理。實現dsd dlm missover的大部分功能。

PROC IMPORT DATAFILE='D:\truncO.csv' OUT=Temp REPLACE;GETNAMES=NO;RUN; 

REPLACE會將輸出數據集替代原有的數據集。

GETNAMES=NO; 是不把第一行當做列名。

DATAROWS = n;是規定從第幾行開始讀取

GUESSINGROWS = n; 是改變默認掃面的行數

 

6:自己定義缺失值

data res;

missing N R;*表示將N R也默認為缺失值,當讀到N R數據時,而要求是數字格式的話,那么不會將其賦值為缺失值,而會寫成N R;

...................

run;


免責聲明!

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



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