Writing out every integer in an IN list (e.g., if Age in (18, 19, 20... 25)) is error-prone and hard to read. A cleaner, more robust approach is to use compound comparison operators: if 18 <= Age <= 25. Or, better yet, use User-Defined Formats (PROC FORMAT). By applying a format, you can bin data for display without actually creating new hard-coded text variables, saving memory and keeping your DATA step code much shorter.
Type : CREATION_INTERNE
The examples use generated data (datalines) for the 'creditscores' table which is then processed to create the 'CreditQualify' table.
| 1 | DATA mycaslib.creditscores / sessref=mysession; |
| 2 | INFILE DATALINES; |
| 3 | INPUT Age Credit_Score; |
| 4 | DATALINES; |
| 5 | 20 500 |
| 6 | 30 600 |
| 7 | 40 700 |
| 8 | 50 780 |
| 9 | 60 810 |
| 10 | 70 400 |
| 11 | 19 550 |
| 12 | 33 690 |
| 13 | 48 710 |
| 14 | 62 820 |
| 15 | ; |
| 16 | RUN; |
| 17 | |
| 18 | DATA mycaslib.creditqualify / sessref=mysession; |
| 19 | SET mycaslib.creditscores; |
| 20 | LENGTH Age_Range $8; |
| 21 | IF Age in (18, 19, 20, 21, 22, 23, 24, 25) THEN Age_Range="18-25"; |
| 22 | ELSE IF Age in (26, 27, 28, 29, 30, 31, 32, 33, 34, 35) THEN Age_Range="26-35"; |
| 23 | ELSE IF Age in (36, 37, 38, 39, 40, 41, 42, 43, 44, 45) THEN Age_Range="36-45"; |
| 24 | ELSE IF Age in (46, 47, 48, 49, 50, 51, 52, 53, 54, 55) THEN Age_Range="46-55"; |
| 25 | ELSE IF Age in (56, 57, 58, 59, 60, 61, 62, 63, 64) THEN Age_Range="56-64"; |
| 26 | ELSE IF Age>=65 THEN Age_Range="65+"; |
| 27 | LENGTH FICO_Rating $12; |
| 28 | IF 300<=Credit_Score<=570 THEN FICO_Rating="Very Poor"; |
| 29 | ELSE IF 580<=Credit_Score<=669 THEN FICO_Rating="Fair"; |
| 30 | ELSE IF 670<=Credit_Score<=739 THEN FICO_Rating="Good"; |
| 31 | ELSE IF 740<=Credit_Score<=799 THEN FICO_Rating="Very Good"; |
| 32 | ELSE IF Credit_Score>=800 THEN FICO_Rating="Exceptional"; |
| 33 | LENGTH Credit_Qualification $12; |
| 34 | IF Credit_Score>=740 THEN Credit_Qualification="Platinum"; |
| 35 | ELSE IF 650<=Credit_Score<=739 THEN Credit_Qualification="Gold"; |
| 36 | ELSE IF 450<=Credit_Score<=649 THEN Credit_Qualification="Secured Card"; |
| 37 | ELSE IF Credit_Score<=449 THEN Credit_Qualification="N/A"; |
| 38 | RUN; |
| 1 | PROC CAS; |
| 2 | TABLE.fetch/TABLE="creditqualify" |
| 3 | index=false; |
| 4 | QUIT; |