1、超经典的SAS BASE的笔记3Topic: Managing Data1.Conditionally IF-THEN-ELSE execute SAS statements2.SORT observations in a SAS data set3.KEEP, DROP, DELETE, RENAME, RETAIN, BY, FILE, PUT and OUTPUT Statements4.PROC FORMAT5.SAS DO-LOOPS6.SAS Array 1.Conditionally IF-THEN-ELSE execute SAS statementsConditional e
2、xecution of data step program statements is implemented using the IF/THEN/ELSE statements. Syntax:IF expression THEN statement1;Observe that IF/THEN and ELSE are two separate SAS statements. Each time the IF statement is executed the expression following the IF is evaluated. When the expression is t
3、rue for the observation, the statement following the THEN is executed. The ELSE statement, which is optional, can be used to control a specific action if the IF condition is false. Or try to fully understand the following statements:The inputs to the IF /ELSE statements are expression is an expressi
4、on that is evaluated for being true or false. statement1 is a statement executed when expression is true. statement2 is a statement executed when expression is false. These examples show different ways of specifying the IF-THEN/ELSE statement. /*Example: IF-THEN*/data if_01_a; input code ; cards;1 2
5、 3;data if_01_b; length type $ 8.; set if_01_a; if code=1 then type =Fix; if code=2 then type =Variable; if code =1 and code =2 then type = Unknown; label type =Types of Mortgage Rate;run;/*Example: IF-THEN/ELSE*/data if_01_c; length type $ 8.; set if_01_a; if code=1 then type =Fix; else if code=2 t
6、hen type =Variable; else if code =1 and code =2 then type = Unknown; label type =Types of Mortgage Rate;run;Let us see how to divide age group?data age_grp; input pat_id age ; cards;290156 66 299871 68 280256 64 270456 60 262156 58263256 55 266456 53 250656 44 251256 43 257456 47258356 48 244606 42
7、249456 41 233256 33 237656 37228356 28 222606 22 219856 21 ;run;data age_grp2; length agegrp $ 5.; set age_grp; if 20= age 35 then agegrp = 20-34; else if 35= age 50 then agegrp = 35-49; else if 50= age 65 then agegrp = 50-64; else if 65 84 then output ; end;run;data oss_test2; set oss_test; if scho
8、ol = NT then do; if course =Math then do; if score 86 then output; end; end;run; Earlier you learned to assign values conditionally using IF-THEN/ELSE statements. You can also use SELECT groups in DATA steps to perform conditional processing. A SELECT group contains these statements:This statement.P
9、erforms this action.SELECTbegins a SELECT group.WHENidentifies SAS statements that are executed when a particular condition is true.OTHERWISE (optional)specifies a statement to be executed if no WHEN condition is met. ENDends a SELECT group. Syntax: SELECT ;WHEN-1 (when-expression-1 ) statement;WHEN
10、-n (when-expression-1 ) statement;END; Example:data sel; input id salary gender job $;cards;1 2800 0 CA2 3100 0 RN3 2698 1 ME4 4550 1 MD5 3895 1 TA;data sel2; length occupation $ 20 Sex $ 7; set sel; select (id); when (1) income=salary*10; when (3,4) income=salary*15; otherwise income=salary*5; end;
11、 select(job); when (CA) occupation=Chartered Accountant; when (RN) occupation=Registed Nurse; when (ME) occupation=Mechanic I; when (MD) occupation=Doctor; otherwise occupation=Other; end; select(gender); when (0) Sex=Female; when (1) Sex=Male; otherwise Sex=Unknown; end;run;2.SORT observations in a
12、 SAS data setBasic Concept:2.1.1 Sorting Orders for Numeric VariablesFor numeric variables, the smallest-to-largest comparison sequence is 1.SAS System missing values (shown as a period or special missing value) 2.negative numeric values 3.zero 4.positive numeric values. data sorting_1; input x ; ca
13、rds;. 1 0 -2 3;proc sort data =sorting_1; by x;run;order of output: . -2 0 1 3;2.1.2 Sorting Orders for Character Variablesdata sorting_2; input area $ ; cards;toronto London 535 . hamilton;proc sort data =sorting_2; by area;run;Output: blank , 535, London, hamilton, toronto2.2 What can SORT do?- Sp
14、ecify the input data set- Create an output data set- Specify the output order- Eliminate duplicate observations with common BY values and other options2.3 Applications of PROC SORT procedureThe sort procedure sorts observations in a SAS data set by one or more character or numeric variables, either
15、replacing the original data set or creating a new, sorted data set. PROC SORT by itself produces no printed output.2.3.1 Observations Sorted by the Values of One VariableIn this example, PROC SORT replaces the original data set, sorted alphabetically by last name, with a data set that is sorted by e
16、mployee identification number. The statements that produce the output follow: data sorting_3;input Name $ IDnumber;datalines;Arnsbarger 5466 Belloit 1988 Capshaw 7338Lemeux 4210 Pierce 5779 Wesley 2092 ;proc sort ; by idnumber;run;2.3.2 Observations Sorted by the Values of Multiple VariablesThe busi
17、nesses in this example are first sorted by town, then by debt from highest amount to lowest amount, then by account number.DESCENDING option: reverses the sort order for the variable that immediately follows in the statement so that observations are sorted from the largest value to the smallest valu
18、e. data sorting_4; input company $ 1-23 town $ 24-36 debt accnt_num; datalines;Apex Catering Apex 37.95 9923 Bobs Beds Morrisville 119.95 4998 Boyd & Sons Accounting Garner 312.49 4762 Deluxe Hardware Garner 467.12 8941 Elway Piano and Organ Garner 65.79 5217 Ice Cream Delight Holly Springs 299.98 2
19、310 Paulines Antiques Morrisville 302.05 9112 Pauls Pizza Apex 83.00 1019Peters Auto Parts Apex 65.79 7288 Strickland Industries Morrisville 657.22 1675 Tinas Pet Shop Apex 37.95 5108 Tims Burger Stand Holly Springs 119.95 6335 Watson Tabor Travel Apex 37.95 3131 World Wide Electronics Garner 119.95
20、 1122 ;run;proc sort ; by town descending debt;run;2.3.3 Create Output Data Set for the Sorted Observationsproc sort data = sorting_4 out = sorting_5; by town descending debt;run;2.3.4 Eliminate duplicate observations-NODUPKEY optionIn this example, PROC SORT creates an output data set that contains
21、 only the first observation of each BY group. The NODUPKEY option removes an observation from the output data set when its BY value is identical to the previous observations BY value. The resulting report contains one observation for each town where the businesses are located. It automatically elimi
22、nates multiple observations where the By variables have the same value.options nodate pageno=1 linesize=80 pagesize=60;data account; input Company $ 1-22 Debt 25-30 AccountNumber 33-36 Town $ 39-51; datalines;Pauls Pizza 83.00 1019 ApexWorld Wide Electronics 119.95 1122 GarnerStrickland Industries 6
23、57.22 1675 MorrisvilleIce Cream Delight 299.98 2310 Holly SpringsWatson Tabor Travel 37.95 3131 ApexBoyd & Sons Accounting 312.49 4762 GarnerBobs Beds 119.95 4998 MorrisvilleTinas Pet Shop 37.95 5108 ApexElway Piano and Organ 65.79 5217 GarnerTims Burger Stand 119.95 6335 Holly SpringsPeters Auto Pa
24、rts 65.79 7288 ApexDeluxe Hardware 467.12 8941 GarnerPaulines Antiques 302.05 9112 MorrisvilleApex Catering 37.95 9923 Apex;proc sort data=account out=towns nodupkey; by town;run;proc print data=towns; var town company debt accountnumber;title Towns of Customers with Past-Due Accounts;run; - NODUPLI
25、CATE /NODUP/NODUPRECS optionIn this example, the NODUPLICATE option removes observations that have duplicate values with BY value.Example: data sorting_6; input pat_id age; cards;290156 66290156 66280256 64280156 64262156 58263256 55;run;proc sort data =sorting_6 noduplicate out = sorting_7; by age
26、;run;*Nodupkey, nodup, and noduplicate*;data dup; input account_id visit mmddyy10. checking_bal comma9.2 ;datalines;201189 11/11/1998 7,865.28201189 11/28/1998 5,724.02201189 12/08/1998 6,908.98202369 11/11/1998 4,405.18204189 11/28/1998 5,724.02204189 12/05/1998 8,054.32225189 11/28/1998 3,632.85225189
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1