How to import data into SAS?

  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:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      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;

      chevron_right

      
      

      Output:

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

      Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      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;

      chevron_right

      
      

      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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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;

    chevron_right

    
    

    Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.