Summary

This document provides an introduction to SAS programming, covering its history, components, and environment. It highlights SAS as a programming language for data analysis and business intelligence.

Full Transcript

# SAS Programming ## 1 Introduction: SAS is a software package developed by SAS Institute. It is a programming language used for data analysis, a statistical package, a tool for business intelligence, and more. SAS is also called Decision Making Analysis. SAS provides GUI (Graphical User Interfac...

# SAS Programming ## 1 Introduction: SAS is a software package developed by SAS Institute. It is a programming language used for data analysis, a statistical package, a tool for business intelligence, and more. SAS is also called Decision Making Analysis. SAS provides GUI (Graphical User Interface), allowing users to interact with SAS applications through graphical presentation. GUI is used for generating reports, graphs, data summaries and statistical tasks. SAS is platform independent, which means we can run SAS on any Operating System such as Linux or Windows. | DATA_SOURCE | DATA_CONSOLIDATION | DATA_STORAGE | DATA_PROVISIONING | |---|---|---|---| | SQL Server | EXTRACT | Data Warehouse | SAS VISUAL ANALYTICS | | X | TRANSFORM | | | | A | | | | | ORACLE | | | | ### Data Source: Data is available in electronic files or software files. Software data is available up to 52 electronic files also called data base files. Example: MS Access, EXCEL, SQL Server, ORACLE DB Server, etc. ### Data consolidation: Data consolidation refers to the collection and integration of data from multiple sources into a single destination. During this process, different data sources are put together, or consolidated, into a single data store. ### Data Warehouse: A data warehouse (DW) is a collection of data derived from different sources. A data warehouse is a relational database that is designed for data consolidation, analysis, and reporting at different aggregate levels. ### Data Provisioning: Designed for accessing, analyzing, and reporting on data for a wide variety of applications. ## 1.1 History of SAS: James Goodnight is the father of SAS programming. While a graduate student in statistics at North Carolina State University colleagues (NCSU), James Goodnight discovered a tool used with 'c' language and wrote a computer program for analyzing agricultural data in 1959. In 1970, it was launched in New York as statistical analysis tools. In 1976, Jim decided to work at developing and marketing his product on a full-time basis, and SAS Institute was founded. In 1980, it changed as software graphical report. In 1990, it changed as a Data warehousing tool. Today, SAS Institute is the world's largest privately-held software company, and Dr. James Goodnight is its CEO. He continues to be actively involved as a developer of SAS System software. ## 1.2 SAS Components: In SAS, we have more than 200 components available. | S.N. | SAS_Components | |---|---| | 1 | Base SAS - Basic Procedures and data management | | 2 | SAS/STAT - Statistical Analysis | | 3 | SAS/GRAPH - Graphical and Presentation | | 4 | SAS/OR - Operations Research | | 5 | SAS/ETS - Econometrics and Time Series Analysis | | 6 | SAS/IML - Interactive Matrix Language | | 7 | SAS/AF - Application Facility | | 8 | SAS/QC - Quality control | | 9 | SAS/INSIGHT - Data Mining | | 10 | SAS/PH - Clinical Trial Analysis | | 11 | SAS/Enterprise Miner - Data Mining | | 12 | SAS/Enterprise Guide - GUI based code editor & Project Manager | | 13 | SAS EBI - Suite of Business Intelligence Application | | 14 | SAS Grid Manager - Manager of SAS grid computing environment | ## 1.3 SAS WINDOW ENVIRONMENT: In Window SAS, help the programmers to reduce the time of writing the codes by using utilities available in SAS window. There are 5 basic parts in SAS Window: - Project Tree - Menu - Search Box - Editor Window - Explorer Window - Log Window - Test ### Editor Window: - This window is a text editor, use to write all the codes and submit SAS programs. (The enhanced editor is syntax sensitive and color codes your programs, making it easier to read and find mistakes.) ### Log Window: - The Log Window contains notes about your SAS session, errors, or warnings associated with your program once submitted. ### Output Window: - The Output Windows generate output of your program. ### The Result window: - The Result Window is the table of contents for your output window. This Window took advantage of GUI used for generating reports, graphs, data summaries, and statistical tasks. ### Explorer Window: - The Explorer Window gives you way access to your SAS files and Libraries. The Explorer Window contains Libraries, file shortcuts, My Computer, Favorite folders. ## 1.4 Libraries in SAS: - Libraries are storage location in SAS Data Sets. We can create a library and save library. A SAS library is only 8-character long. The default libraries in SAS: - | Library | Description | |---|---| | SASHELP | The SASHELP library contains information that controls your SAS session along with sample SAS DATASETS. | | SASUSER | If you make changes in the default settings for SAS programming environment, this information will be store in the SASUSER library. You can also store SAS data. SAS programs and other SAS files in the SASUSER libraries. | | WORK | The WORK library is a temporary storage location for SAS data sets. It is also the default library. If you create as SAS data set without specifying a library, SAS will put it in the WORK library. | ## 1.5 SAS PROGRAMS A SAS program is a sequence of statements executed in order. A statement gives information or instruction to SAS and must be approximately places in the program. ### SAS program structure: - There are two steps: 1. DATA Steps: - DATA SET are nothing but tables. In DATA SET rows are called OBSERVATIONS & columns are called as VARIABLES. - It starts with Data statement. - It reads and modifies the data. - It creates a SAS DATA SET. 2. PROC Steps: - This step involves invoking a SAS built-in procedure to analyze the data. - It starts with PROC Statement. - It performs specific analysis or function. - It produces results or reports. A step ends when SAS encounters a new step (denoted by a DATA or PROC statement), a RUN statement tells SAS to run all the preceding lines of the step. Instead of RUN, we can use QUIT, STOP, or ABORT Statements. ## To write a DATA SET on PROC SET in any program in SAS environment, we must follow below rules: - 1. SAS File Name and Variable Name - We can give upto 32 characters. - Names start with letters or underscore (-). - We can use letters, numbers. - We can't use any special characters except underscore. 2. SAS Statement Rules: - - Every statement must end with a semicolon (;). - SAS statement can be in upper and lower case. - One statement can continue in different statement lines, or different statements can continue on one line. - Statements can start in any column. 3. SAS Names Rules: - - Names must be 32 characters or fewer in length. - Names must start with a letter or an underscore (_). - Names can contain only letters, numerals, or underscores (_). - Names can contain upper-lower case letters. - Don't split words. 4. DATA types: - There are two Data types: - **Numeric:** Numeric fields are numbers. They can be added and subtracted, can have any number of decimal places, and can be positive or negative. In addition to numerals, numeric fields can contain plus (+), minus sign (-), decimal point (.) or E for scientific notation. - **Character:** Character data are everything else and contain $ or !. Characters can be up to 3276 characters long. 5. Raw Data types: - There are two types: - **Internal**: When data are available in the SAS environment, then SAS treats them as internal raw data. To read internal raw data, we have to use CARDS or DATALINES Keyword. - **External**: When data are available in the System environment (other than the SAS environment), then the data is External to SAS. To read external raw data, we use INFILE keyword. ### Comments: - Comments make your program more understandable. You can insert comments in your programs. There are two styles of comment that you can use: - One starts with an asterisk (*) and ends with a semicolon (;). - The other style starts with (/*) and ends with (*/). ### Shortcut for Comment line: - To comment: CTRL + ? - To uncomment: SHIFT + CTRL + ? ## Level of Dataset name: - ### Single level of data set name: If you use the single level of data set name. The data set default stores in the working library. ```SAS data demol; input Id Names Age; cards; 1 Gnana 25 2 Arun 24 3 Joseph 27 ; run; ``` ### Two level of data set name: Two levels of data set name, we can store data in the required name. ```SAS data work.demol; set sashelp.cars; run; ``` # 2. SAS / BASE It is one of the modules in base SAS tools, and it is used to create SAS files (SAS data set). ## 2.1 INPUT STATEMENT STYLES AND OPTIONS: The INPUT statement tells SAS how to organize the table or how to read the raw data. Basically, we have six types of INPUT styles: 1. LIST INPUT 2. COLUMN INPUT 3. FORMATTED INPUT 4. NAME INPUT 5. ABSOLUTE INPUT 6. MIXED INPUT ### 1) LIST INPUT: Data values are separated by at least one blank space. Any character data values do not have embedded space. In this case, we use a list input method. - LIST INPUT is the easiest way to read raw data into SAS. - It reads only 8 characters in length. - Missing numeric values will appear as a period. - Missing character values will appear as spaces. - This is also called "Free Formatted Input." ```SAS data ListInput; length Name $20; input Name$ Age Gender$; cards; Gnana 23 M Nikshitha 1 F Arun 26 M ; run; ``` ### Length statement: Using a length statement, we can increase the storage for character variables. We can increase the storage capability up to 256 characters. ### 2) COLUMN INPUT: In raw data, data values are available in specific columns. - You can read n number columns. - You can skip unwanted variables. - Character data can have embedded spaces. - Missing values can be left blank. - Spaces are not required between values. ```SAS data Column Input; input name$1-6 age 7-9 gender$10-11; cards; Gnana 23 M Usha 24 F Arun 26 M ; run; ``` ### 3) FORMATTED INPUT: In formatted input methods, 2 symbols play a main role: - +n indicates the column pointer - non-required data. - n. indicates the the column range - required data. - Syntax: - `Input +n <variable name> <data type> n;` ```SAS data Formatted Input; Input +0 Pid 3. +1 Name$12. +1 Age 2. +2 Color $5.; Cards; 100 Gnana Murthy 89 white 101 Ambika 67 black 102 Usha Arun 89 white ; run; ``` ### 4) NAME INPUT: Sometimes, in raw data, data values are available with variables names. In this case, we can use a named input method. ```SAS data NamedInput; Length Name $13; Input Id- Age Name-; cards; Id-001 Age-23 name-Joseph Id-002 Age-34 name-Arun Id=003 Age-45 name-Harish ; run; ``` ### 5) ABSOLUTE INPUT: Using absolute input methods, we can read standard and non-standard data. - @n--column hold pointer - n - column range - n - indicates column number Syntax: `Input @n <variable Name> <data type> n;` ```SAS data AbsoluteInput; input @1 Id 3. @5 Name $7. @12 Age 2.; Cards; 100 Dhana 89 101 Geetha 67 102 Hema 89 ; run; ``` ### 6) MIXED INPUT: If we use or write one or more input techniques in the required input statement, then it is called a mixed input statement. ```SAS data MixedInput; input (1 name $6. +0 age 2. gender$10-11; cards; Gnana 23 M Usha 24 F Arun 26 M ; run; ``` ## Advanced Input methods: ### 1) Column Pointer: #### (a) @n:- This option can be used anytime you want SAS to "Skip" backward or forward within a data line. The `N` value is from 1 to n: ```SAS INPUT @3 NAMES AGE GENDERS; ``` ```SAS data Column Pointer; input (3 names age gender$; cards; Gnana 23 M Arun 24 M Usha 25 F Ambi 26 F ; run; ``` #### (b) @'character' You want to skip unwanted characters and symbols by using this column pointer. ```SAS INPUT `@'SIR' NAME$;` ``` ```SAS data ColumnPointer; input ('Mr.' name $6. age gender$; cards; Mr. Gnana 23 M Mr. Arun 24 M ; run; ``` #### (c) Line Pointer We can write multiple INPUT statements to read each record that comprises a single observation. There are two types of line pointer controls: - The forward slash (/) specifies a line location relative to the current one. - #n specifies the absolute number of the line to which you want to move the pointer. ```SAS data LinePointer; input #1 Lname $ 1-8 Fname $ 10-15 #2 Department $ 1-12 JobCode $ 15-19 #3 Salary comma10.; Cards; Murthy Gnana Statistic CL01 $25,000.00 P Arun Education IN01 $26,000.00 ; run; ``` ```SAS data LinePointer; input Lname $ 1-8 Fname $ 10-15 / Department $ 1-12 JobCode $ 15-19 / Salary comma10.; Cards; Murthy Gnana Statistic CL01 $25,000.00 P Arun Education IN01 $26,000.00 ; run; ``` ### 2) Colon Modifier: Whenever you have raw data more than 8 characters and datelines shorter than others, use a colon modifier after the variable name in the input statement to read the data perfectly. It is used to increase storage capability without changing the order of data. ```SAS data ColonModifier; input Name: $12. Age Gender$; cards; GnanaMurthy 25 M Arun 24 M Joseph 25 M ; RUN; ``` ### 3) Ampersand Modifier: It is used to indicate one or more blank spaces; a delimiter between the required data values. ```SAS DATA AmpersandModifer; INPUT NAME&$14. AGE GENDER$; /*Value after Gnana Murthy and before 23 has two spaces. Two spaces is delimeter*, CARDS; Gnana Murthy 23 M Usha Arun 27 M Joseph Barnad 32 M ; RUN; ``` ### 4) Tilde Modifier: Tilde modifier can be used to avoid DSD option for the required variable and enables you to read and retain single quotation marks, double quotation marks, and delimiters within character values. ```SAS data TildeModifier; infile datalines dsd; input Id: $1. Name 59. Skills $25.; datalines; 1, Gnana, "ASP.NET, SAS" 2,Arun, "Java Spring" 3, Joseph, ". Net MVC, Javascript" ; run; ``` ### 5) Double Trailing When you have multiple observations, use this option at the end of the INPUT statement. ```SAS DATA DoubleTrailer; INPUT NAME: $12. AGE GENDER$@@; CARDS; GnanaMurthy 23 M Arun 27 M Joseph 32 M ; RUN; ``` ### 6) Single Trailing: - When you want to read part of the raw data, use this option after the particular variable in the INPUT statement. (Single Trailing holds the data). ```SAS data SingleTrailer; input Name$ ; if Name = 'Joseph' or Name 'Nikshi' = then delete; input age gender$; cards; Arun 20 M Usha 25 F Joseph 34 M Nikshi 30 F Murthy 23 M ; run; ``` ## 2.2 INFILE STATEMENT: The INFILE statement follows the DATA statement and must precede the INPUT statement. After the INFILE keyword, the file path and name are enclosed in quotation marks. Used to accept data from external files (txt) and stores SAS files. **Syntax:** ```SAS DATA DATA-SET-NAME; INFILE "PATH (The location of the raw data)"; INPUT VARIABLE-LIST; RUN; ``` ```SAS data ReadFile; infile "c:/file.txt"; Input Id $ Product $ Price Qty Total; run; ``` **Note:-** - INFILE statement tells SAS the location of the raw data. **SAS PROGRAMMING:** - INPUT - To organize data - RUN - Run all preceding lines ## SAS Data Reading Pattern: Reading and understanding the SAS data reading pattern can sometimes be a challenge. This is evident in the INFILE statement. This can get very sticky when the data file you need to read differs from the safe, easy columnar data files. SAS reads text files with spaces as delimiters. Whenever missing values are found, it skips and reads the next value, or removes the observation to save the next observation in the dataset. **Ex 1:** ```SAS data ReadFile; infile "c:\file2.txt",; Input Name $ Gender $ Dept $; run; ``` | Name | Gender | Dept | |---|---|---| | A | M | CS | | B | F | MCA | | C | ISE | D | |E | F | MBA | | F | CIVIL | **G** | **C** - Character Variable **N** - Numeric Variable **-** - Removed index The above example illustrates that the Gender value is missing; it takes the next value from Dept, and for Dept, it takes the next value from the next observation. It removes observations (highlighted *read*) to save the next observation. **Ex 2:** | Name | Age | Gender | |---|---|---| | A | 19 | M | | B | 30 | F | | C | M | | D | M | | E | 39 | F | | F | 40 | F | | G | M | | Name | Age | Gender | |---|---|---| | A | 19 | M | | B | 30 | F | | C | M | | D | 39 | F | | E | 40 | F | | F | M | | G | M | **C** - Character Variable **N** - Numeric Variable **-** - Removed index The above example illustrates that the Age (Note: Numeric Variable) value is missing; it takes the next value from Gender. Numeric variables can't read character data, so they put a dot (.) as missing. Gender takes the value from the next observation and removes it to save the next observation. ## INFILE OPTIONS: Using INFILE options, we can read the data in proper order. 1. **FIRSTOBS** 2. **OBS** 3. **MISSOVER** 4. **DLM** (delimiter) 5. **DSD** (data separated delimiter or data specified delimiter) 6. **FLOWOVER** 7. **STOPOVER** 8. **TRUNCOVER** 9. **SCANOVER** 10. **LRCEL** (Long Record Length or Logical Record Length) ### 1. FIRSTOBS: This option tells SAS at what line to begin reading data. ```SAS data ReadFile; infile "c:/file.txt" FIRSTOBS-3; ``` ### 2. OBS: This option tells SAS to stop reading when it gets to that line in the raw data file. ```SAS data ReadFile; infile "c:/file.txt" OBS=3; Input Id $ Product $ Price Qty Total; run; ``` ```SAS data ReadFile; infile "c:/file.txt" FIRSTOBS-2 OBS-4; Input Id $ Product $ Price Qty Total; run; ``` ### 3. MISSOVER: This 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. **Ex 1:** | Name | Age | Gender | |---|---|---| | A | 19 | M | | B | F | | C | 30 | M | | D | M | | E | 39 | F | | Name | Age | Gender | |---|---|---| | A | 19 | M | | B | . | **F** | | C | 30 | M | | D | . | F | | E | 39 | F | **C** - Character **N** - Numeric **Ex 2:** | Name | Age | Gender | |---|---|---| | A | 19 | M | | B | 30 | F | | C | M | | D | M | | E | 39 | F | | Name | Age | Gender | |---|---|---| | A | 19 | M | | B | 30 | F | | C | . | . | | D | . | . | | E | 39 | F | **C** - Character **N** - Numeric ```SAS data ReadFile; infile "c:/file2.txt" missover; Input Name & Age Gender $; run; ``` ### 4. DLM (Delimiter): When you have a delimiter (a special character that separates the data values) in the file or are used to indicate the delimiter in raw data. **Note:** Commas (DLM=",") or Tab (DLM="09"X;) characters are common delimiters. **Note:** CARDS statement or DATALINES statements are used to indicate internal data. ```SAS data DIMOption; Infile cards dlm-", &$"; Input Eid name $ age Desig $; Cards; 100, Raj, 78, Admin 101, Joe, 89$Engineer 102&Nik, 78&Assis ; run; ``` ### 5. DSD (Data Separated Delimiter or Data Specified Delimiter or Data Sensitive Delimiter): #### A) Data Separate Delimiter: In raw data, data values are separated by columns. We will use DSD instead of DLM. #### B) Data Sensitive or Data Specified Delimiter: In raw data, character data values are available with quotation marks for manual checking. To read this raw data into SAS files without quotation marks, use a data sensitive delimiter. ```SAS data DSDOption; Infile cards DSD; Input Eid name $age Desig$; Cards; 100, "Raj", 78, Admin 101, Joe,, 102, "Nik", 78, "Assis" ; run; ``` ```SAS data DSDOption; Infile cards DSD dlm=", &S"; Input Eid name $age Desig$; Cards; 100, "Raj"&78$Admin 101, Joe&$ 102, "Nik"$78, "Assis" ; run; ``` ### 6. FLOW OVER: The default nature of SAS is controlled by the system to read one line for one observation. If any value is missing in the raw data, it tries to read the next data value. It may be successful in some cases, but not others. | Missing | Next | Rating | |---|---|---| | Ch | Ch | ✓ | | Ch | N | X | | N | Ch | ✓ | | N | N | ✓ | To resolve SAS flow over, put a dot (.) for missing values. This option tells SAS that the dot is a missing value. ```SAS data FlowOver; infile cards; Input Id $ Product $ Price Qty Total; cards; 1 10000 1 10000 2 Hp 2000 2 4000 3 Lenovo . 3 90000 4 Jio 40000 2 80000 5 5000 3 15000 ; run; ``` ### 7. STOP OVER: When an error occurs in the raw data during reading, the stop over nature tries to stop the reading. Errors are of 2 types: - - a) Compilation or syntax error - b) Execution or data error #### Syntax Error: We can identify syntax errors in compilation. - Forget the semicolon at the end of the SAS statement. - Unfortunately, writing a dataset block in a procedure block or a procedure block in a dataset block is not allowed. #### Data Errors: These errors can be identified during execution. - Variables request a numeric value, but you send a character value. - A row or observation indicates 6 data values, but you mention the input statement has 8 data values. ### 8. TRUNCOVER: It works like a MISSOVER. It adjusts any of the length for the required variable and the displace and reduces the storage place: ```SAS data TrunCover; infile cards TRUNCOVER; Input Id $ Product $ Price Qty Total; cards; 1 10000 1 10000 2 Hp 2000 2 4000 3 Lenovo 3 90000 4 Jio 40000 2 80000 5 5000 3 15000 ; run; ``` ### 9. SCANOVER: Use this option to search the data lines for the character string specified in the INPUT statement. It is used only for character data, not numeric data. ```SAS data ScanOver; Infile cards. scanover; Input @'1' Name $ Age Gender $; Cards; 1 A 18 M 2 B 30 F 3 C 34 M 4 A 56 F 1 E 23 M 6 B 33 F 7 D 45 M ; run; ``` ### 10. LRCEL (Long Record Length or Logical Record Length): If your data lines are long, and it looks like SAS is not reading all of your data, then use the LRECL option in the INFILE statement to specify the record length at least as long as the longest record length in your data file. (Includes spaces and characters) The default LRECL value is 256. **Example:-** ```SAS DATA ONEDATA; INFILE "C:/SAS/A.TXT" DLM="," MISSOVER DSD LRECL=100; INPUT NAMES AGE GENDERS; RUN; ``` ## 2.3 USING IF-THEN CONDITIONAL STATEMENT Frequently, you want an assignment statement to apply to some observations but not all under some conditions, but not others. This is called conditional logic, and you do it with IF-THEN statements. **Note:** All of these IF conditional statements can only be requested in the DATA step. By using conditional statements, we can create: - New variables - Delete observations - Change existing variable values, and perform subsetting processes. **Syntax: -** `IF Condition THEN Action;` ### Create Variable with IF Condition: ```SAS DATA ONE; INPUT NAMES AGE GENDERS; IF NAME="B" THEN SALARY=5000; CARDS; A 33 M B 38 F C 47 M D 29 F ; RUN; ``` ### Delete Observation with IF-THEN condition: ```SAS DATA ONE; INPUT NAMES AGE GENDERS; IF GENDER="F" THEN DELETE; CARDS; A 33 M B 38 F C 47 M D 29 F E 30 M ; RUN; ``` ### Change the existing observation value and subsetting process: ```SAS DATA ONE; INPUT NAMES AGE GENDERS; IF NAME=="A" THEN NAME="2"; IF GENDER="F" THEN SALARY=14000; IF GENDER=="M" THEN SALARY=15000; CARDS; A 33 M B 38 F C 47 M D 29 F E 30 M ; RUN; ``` ## SAS PROGRAMMING ### MNEMONIC OPERATORS: | Operator | Symbolic | Mnemonic | Meaning | |---|---|---|---| | Conditional Operator | = | EQ | Equals | | Conditional Operator | != or ^= | NE | Not Equal | | Conditional Operator | > | GT | Greater Than | | Conditional Operator | >= | GE | Greater Than Equal | | Conditional Operator | < | LT | Less Than | | Conditional Operator | <= | LE | Less Than Equal | | Logical Operator | & | AND | All comparisons must be true | | Logical Operator | | OR | Only one comparison must be true | | Logical Operator | | BETWEEN | Between values | | Logical Operator | | IN | Where Observation IN | | Logical Operator | | NOT IN | Where Observation NOTIN | | Logical Operator | | LIKE | Where Observation LIKE | ## DIFFERENT CONDITIONAL STATEMENTS: 1. IF ELSE Statements 2. IF ELSE IF ELSE IF .... ELSE Statements 3. IF CONDITION (Sub-Setting) 4. IF-THEN DO LOOPS 5. ITERATIVE DO LOOPS 6. DO WHILE LOOP 7. DO UNTIL LOOP 8. WHERE Statement (Expression) ### 1. IF ELSE Statements: IF-ELSE statements are conditional statements that execute an action. If the condition is true, the action is executed. If the condition is false, control passes to the else statement, and the action is executed. The else statement is an optional statement. **Syntax: -** `IF condition THEN action; ELSE action;` ```SAS DATA ONE; INPUT NAMES AGE GENDERS SALARY; IF SALARY <2000 THEN SALARY=SALARY + 500; ELSE SALARY=SALARY+1000; CARDS; A 33 M 1000 B 38 F 2000 C 47 M 1500 D 29 F 5000 E 30 M 1000 ; RUN; ``` ### 2. IF ELSE IF ELSE IF .... ELSE Statements: In this case, the IF statement is followed by several else if statements. This can be very useful if you want to test various types of scenarios and situations. **Syntax: -** `IF condition THEN action; ELSE IF condition THEN action; ELSE IF condition THEN action; ELSE action;` ```SAS DATA ONE; INPUT NAMES AGE GENDERS SALARY; IF SALARY > 1000 AND SALARY <=2000 THEN SALARY=SALARY + 500; ELSE IF SALARY > 2000 AND SALARY <=3000 THEN SALARY=SALARY+1000; ELSE SALARY=SALARY +800; CARDS; A 33 M 1000 B 38 F 2000 C 47 M 1500 D 29 F 3000 E 30 M 5000 ; RUN; ``` ### 3. IF CONDITION (Sub-Setting): IF-CONDITION or EXPRESSION execute a single action. **Syntax: -** `IF EXPRESSION;` ```SAS DATA ONE; INPUT NAMES AGE GENDER$ SALARY; IF GENDER=="M"; CARDS; A 33 M 1000 B 38 F 2000 C 47 M 1500 D 29 F 3000 E 30 M 5000 ; RUN; ``` ### 4. IF-THEN DO LOOPS: We can execute more than one action by adding the keywords DO and END. **Syntax: -** `IF condition THEN DO; Action1; Action2; Action-n; END;` ```SAS DATA ONE; INPUT NAMES AGE GENDERS; IF GENDER="F" THEN DO; SALARY = 5000; BONUS=1000; TOTAL=SALARY+BONUS; END; ELSE DO; SALARY=8000; BONUS=2000; TOTAL=SALARY+BONUS; END; CARDS; A 33 M B 38 F C 47 M D 29 F E 30 M ; RUN; ``` ### 5. ITERATIVE DO LOOPS: Iterative DO loops are the simplest form of DO loops that can be executed within the SAS Data Step. Basic iterative DO loops are most useful for incremental counting or repetitive calculation exercises. ```SAS data IterLoop; doi 1 to 6; Increment + 1000; output; end; run; ``` | Increment | |---|---| | 1 | 1000 | | 2 | 2000 | | 3 | 3000 | | 4 | 4000 | | 5 | 5000 | | 6 | 6000 | ### 6. DO WHILE LOOP: - The DO WHILE() will test before executing the code in the loop. - DO WHILE() continues when the condition is TRUE. - DO WHILE, the loop executes 0 -n times. ```SAS Data DoWhile (Drop=I); I=1; Do while(i<=10); SeqNo = 1; output; I=i+1; End; Run; ``` | SeqNo | |---|---| | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 | 5 | 5 | 6 | 6 | 7 | 7 | 8 | 8 | 9 | 9 | 10 | 10 ### 7. DO UNTIL LOOP: - DO UNTIL() will test after executing the code. - DO UNTIL() continues when the condition is FALSE. - DO UNTIL, the loop executes 1 - n times

Use Quizgecko on...
Browser
Browser