Open In App

How to import data into SAS?

Last Updated : 23 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report
  1. Entering Data Directly: You can enter numbers of lines of data directly in SAS program by using a DATALINES statement.

    The keywords are as follows:

    • DATA: The DATA step always starts with a DATA statement. The purpose of the DATA statement is to tell SAS that you are creating a new data set i.e. outdata.
      DATA outdata;
      
    • INPUT: To define the variables used in data set.

      INPUT age gender $ dept obs1 obs2 obs3; 
      
    • Dollar sign ($): To declare variable as a character.

      INPUT age gender $ dept obs1 obs2 obs3; 
      
    • DATALINES: To refer that lines following DATALINES statement a real data.

      DATALINES; 
      
    • PROC PRINT: To display out the contents of data set in output window.

      proc print;
      
    • RUN: The DATA step ends with a RUN statement to run the complete code.

      run;
      
    • Example:




      DATA outdata; 
         INPUT age gender $ dept obs1 obs2 obs3; 
         DATALINES; 
      25 M 3 17 6 24
      24 F 1 19 25 7
      31 F 4 24 10 20
      33 M 2 19 23 8
      22 M 1 14 23 12
      22 F 5 1 23 9
      31 M 1 8 21 7
      34 M 1 7 7 14
      31 F 2 2 1 22
      22 F 5 20 5 2
      32 M 4 21 8 18
      41 M 4 7 9 25
      24 M 5 10 17 20
      31 F 4 21 25 7
      32 M 3 9 9 5
      ;
      proc print;
      run;

      
      

      Output:

    • You can also use CARDS instead of DATALINES. Both means the same. There is no difference between these two keywords.

      Example:




      DATA outdata;
         INPUT age gender $ dept obs1 obs2 obs3;
         CARDS; 
      24 F 1 19 25 7
      31 F 4 24 10 20
      33 M 2 19 23 8
      22 M 1 14 23 12
      22 F 5 1 23 9
      31 M 1 8 21 7
      ;
      proc print;
      run;

      
      

      Output

  2. Reading Delimited Data:

    The default delimiter is blank. If you have a data file with other delimiters such as comma or tab you need to define the delimiter before defining the variables using INFILE and DLM = options.

    Syntax:

    Infile 'file-description' dlm=', '
    1. While using tab delimiter, the syntax would be

      infile 'file-description' dlm='09'x
    2. While using colon delimiter, the syntax would be

      infile 'file-description' dlm=':'

    Example:




    DATA outdata; 
       INFILE Datalines dlm =", ";
       INPUT age gender $ dept obs1 obs2 obs3; 
       Datalines; 
    34, M, 1, 7, 7, 14
    31, F, 2, 2, 1, 22
    22, F, 5, 20, 5, 2
    32, M, 4, 21, 8, 18
    41, M, 4, 7, 9, 25
    24, M, 5, 10, 17, 20
    ;
    proc print;
    run;

    
    

    Output:



Similar Reads

How to import External Data from Excel or Text file into SAS Programming?
PROC IMPORT: It is a procedure to import external files into SAS. It automates importing process. There is no need to specify the variable type and variable-length to import an external file. It supports various formats of files such as excel, txt, etc. Importing an Excel File into SAS: The main keywords used in the following program are: OUT: To s
2 min read
How to Create or Modify a Variable in SAS Programming?
This will help you to create or modify a variable. It is common to define a new variable based on the existing variable. Let's create a dataset In the code below, we are creating a dataset named as Example1 which is going to store on WORK(temporary) library. In this dataset, there would be a variable called OldRate which contains a numeric value. T
2 min read
Contains and Between-And operators in SAS Programming
BETWEEN-AND Operator: Between Two Numbers Task 1: Suppose you want to select scores whose values are greater than or equal to 50 and less than or equal to 90. data readin; input name $ Section $ Score; cards; Raj A 80 Atul A 77 Priya B 45 Sandeep A 95 Rahul C 84 Shreya C 44 ; run; data readin1; set readin; where Score between 50 and 90; run; where
1 min read
How to drop variables from a dataset in SAS Programming?
This topic is regarding how to drop variables from a dataset in SAS. It includes various methods to delete variables from data. In SAS, there are two ways to drop variables: DROP = data set option DROP statement Let's start with creating a data set: DATA outdata; INPUT roll_num gender $ class subj1 subj2 subj3; DATALINES; 21 F 6 10 17 20 13 F 6 21
2 min read
Where Statement in SAS Programming
The WHERE statement is a substitute to IF statement when it comes to subsetting a data set. Syntax: WHERE (condition is true) => It refers to subsetting a dataset. Task1 : Suppose you want to select only section A students. You need to filter Section variable equals to A using where clause. data readin; input name $ Section $ Score; cards; Raj A
2 min read
If-Then-Else statement in SAS Programming
Comparison Operators used while using conditional statements. Symbol Mnemonic Meaning = EQ equals ^= or ~= NE not equal > GT greater than < LT less than >= GE greater than or equals <= LE less than or equals in IN selecting multiple values IF statement Syntax: IF (condition is true) => It means subsetting a dataset. Example: Data rea
3 min read
IS MISSING and IS NOT MISSING Operators in SAS Programming
IS MISSING Operator: Selecting Missing Values Task 1: Suppose you want to select only those observations in which students did not fill their section information. data readin; input name $ Section $ Score; cards; Raj A 80 Atul . 77 Priya B 45 Sandeep A 95 Rahul . 84 Shreya . 44 ; run; data readin1; set readin; where Section is missing; run; Output:
1 min read
Like Operator in SAS Programming
LIKE Operator: Pattern Matching The LIKE operator used to select data by comparing the values of a character variable to a specified pattern. It is case sensitive. Task 1: To select all students with a name that starts with the letter S. There are two special characters patterns available for specifying a pattern: percent sign (%) - Wildcard Charac
1 min read
SAS | How to specify a list of Variables?
Suppose you have a list of variables. You don't want to type the name of each variable every time to define them within the function or array. You are looking for a shortcut to accomplish this task. Create a dataset with a list of variables data dummy; input a1 a3 a4 a2 a6$ bs$ a5; cards; 2 1 3 5 aa xy 2 2 5 4 1 ab xz 4 2 7 3 9 ac wx 3 ; run; Outpu
2 min read
SAS | How to read character of varying length using COLON Modifier
We generally face this situation when we have company names or both first and last names of a person in our data set. We can use a colon modifier ":" to tell SAS to read variable "Name" until there is a space or other delimiter. The $30. refers to the variable as a character variable having max length 30. data ex1; input ID Name :$30. Score; cards;
2 min read