1、;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 Raterun; IF-THEN/ELSE*/data if_01_c; else if code=2 then type = else if code =1 and code =2 then type = Let us see how to divi
2、de age group?data age_grp; input pat_id age ;290156 66 299871 68 280256 64 270456 60 262156 58263256 55 266456 53 250656 44 251256 43 257456 47258356 48 244606 42 249456 41 233256 33 237656 37228356 28 222606 22 219856 21 data age_grp2; length agegrp $ 5.; set age_grp; if 20= age 35 then agegrp = 20
3、-34 else if 35 50 then agegrp = 35-49 else if 50 65 then agegrp = 50-64 else if 65 84 then output ; end;data oss_test2; if school = NT if course =Math if score 86 then output;Earlier you learned to assign values conditionally using IF-THEN/ELSE statements. You can also use SELECT groups in DATA step
4、s to perform conditional processing. A SELECT group contains these statements:This statement.Performs 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 condit
5、ion is met. ENDends a SELECT group. SELECT WHEN-1 (when-expression-1 ) statement;WHEN-n (when-expression-1 OTHERWISE statement;END;Example:data sel; input id salary gender job $;1 2800 0 CA2 3100 0 RN3 2698 1 ME4 4550 1 MD5 3895 1 TAdata sel2; length occupation $ 20 Sex $ 7; set sel; select (id); wh
6、en (1) income=salary*10; when (3,4) income=salary*15; otherwise income=salary*5; select(job); when (CA) occupation=Chartered AccountantRNRegisted NurseMEMechanic IMDDoctor otherwise occupation=Other select(gender); when (0) Sex=Female when (1) Sex=Male otherwise Sex=Basic Concept:2.1.1 Sorting Order
7、s 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;. 1 0 -2 3proc sort data =sorting_1; by x;order of output:
8、. -2 0 1 3;2.1.2 Sorting Orders for Character Variablesdata sorting_2; input area $ ;toronto London 535 . hamiltonproc sort data =sorting_2; by area;Output: blank , 535, London, hamilton, toronto2.2 What can SORT do?- Specify the input data set- Create an output data set- Specify the output order- E
9、liminate 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 replacing the original data set or creating a new, sorted data set. PROC SORT by
10、 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 employee identification number. The statements that produce the output follow:dat
11、a sorting_3;input Name $ IDnumber;datalines;Arnsbarger 5466 Belloit 1988 Capshaw 7338Lemeux 4210 Pierce 5779 Wesley 2092 proc sort ; by idnumber;2.3.2 Observations Sorted by the Values of Multiple VariablesThe businesses in this example are first sorted by town, then by debt from highest amount to l
12、owest 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 value. data sorting_4; input company $ 1-23 town $ 24-36 debt accnt_num; datalines;Apex Ca
13、tering 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 2310 Paulines Antiques Morrisville 302.05 9112 Pauls Pizza Apex 83.00 1019Peters Auto P
14、arts 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 1122 by town descending debt;2.3.3 Create Output Data Set for the Sorted Observations
15、proc sort data = sorting_4 out = sorting_5;2.3.4 Eliminate duplicate observations-NODUPKEY optionIn this example, PROC SORT creates an output data set that contains only the first observation of each BY group. The NODUPKEY option removes an observation from the output data set when its BY value is i
16、dentical to the previous observations BY value. The resulting report contains one observation for each town where the businesses are located. It automatically eliminates multiple observations where the By variables have the same value.options nodate pageno=1 linesize=80 pagesize=60;data account; inp
17、ut Company $ 1-22 Debt 25-30 AccountNumber 33-36 Town $ 39-51;s Pizza 83.00 1019 ApexWorld Wide Electronics 119.95 1122 GarnerStrickland Industries 657.22 1675 MorrisvilleIce Cream Delight 299.98 2310 Holly SpringsWatson Tabor Travel 37.95 3131 Apex Sons Accounting 312.49 4762 Garners Beds 119.95 49
18、98 Morrisvilles Pet Shop 37.95 5108 ApexElway Piano and Organ 65.79 5217 Garners Burger Stand 119.95 6335 Holly Springss Auto Parts 65.79 7288 ApexDeluxe Hardware 467.12 8941 Garners Antiques 302.05 9112 MorrisvilleApex Catering 37.95 9923 Apexproc sort data=account out=towns nodupkey; by town;proc
19、print data=towns; var town company debt accountnumber;title Towns of Customers with Past-Due Accounts- NODUPLICATE /NODUP/NODUPRECS optionIn this example, the NODUPLICATE option removes observations that have duplicate values with BY value.data sorting_6; input pat_id age;290156 66280256 64280156 64
20、262156 58263256 55proc sort data =sorting_6 noduplicate out = sorting_7; by age ;*Nodupkey, nodup, and noduplicate*;data dup; input account_id visit mmddyy10. checking_bal comma9.2 ;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