How to import data into SAS? - GeeksforGeeks

23 Jul, 2019
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 2424 F 1 19 25 731 F 4 24 10 2033 M 2 19 23 822 M 1 14 23 1222 F 5 1 23 931 M 1 8 21 734 M 1 7 7 1431 F 2 2 1 2222 F 5 20 5 232 M 4 21 8 1841 M 4 7 9 2524 M 5 10 17 2031 F 4 21 25 732 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 731 F 4 24 10 2033 M 2 19 23 822 M 1 14 23 1222 F 5 1 23 931 M 1 8 21 7;proc print;run;OutputReading 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=', 'While using tab delimiter, the syntax would beinfile 'file-description' dlm='09'xWhile using colon delimiter, the syntax would beinfile 'file-description' dlm=':'Example:DATA outdata;    INFILE Datalines dlm =", ";   INPUT age gender $ dept obs1 obs2 obs3;    Datalines; 34, M, 1, 7, 7, 1431, F, 2, 2, 1, 2222, F, 5, 20, 5, 232, M, 4, 21, 8, 1841, M, 4, 7, 9, 2524, M, 5, 10, 17, 20;proc print;run;Output:
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 2424 F 1 19 25 731 F 4 24 10 2033 M 2 19 23 822 M 1 14 23 1222 F 5 1 23 931 M 1 8 21 734 M 1 7 7 1431 F 2 2 1 2222 F 5 20 5 232 M 4 21 8 1841 M 4 7 9 2524 M 5 10 17 2031 F 4 21 25 732 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 731 F 4 24 10 2033 M 2 19 23 822 M 1 14 23 1222 F 5 1 23 931 M 1 8 21 7;proc print;run;Output
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;

DATA outdata;

INPUT: To define the variables used in data set.INPUT age gender $ dept obs1 obs2 obs3; 

INPUT age gender $ dept obs1 obs2 obs3; 

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

INPUT age gender $ dept obs1 obs2 obs3; 

DATALINES: To refer that lines following DATALINES statement a real data.DATALINES; 

DATALINES; 

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

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 2424 F 1 19 25 731 F 4 24 10 2033 M 2 19 23 822 M 1 14 23 1222 F 5 1 23 931 M 1 8 21 734 M 1 7 7 1431 F 2 2 1 2222 F 5 20 5 232 M 4 21 8 1841 M 4 7 9 2524 M 5 10 17 2031 F 4 21 25 732 M 3 9 9 5;proc print;run;Output:
run;

Example:
DATA outdata;    INPUT age gender $ dept obs1 obs2 obs3;    DATALINES; 25 M 3 17 6 2424 F 1 19 25 731 F 4 24 10 2033 M 2 19 23 822 M 1 14 23 1222 F 5 1 23 931 M 1 8 21 734 M 1 7 7 1431 F 2 2 1 2222 F 5 20 5 232 M 4 21 8 1841 M 4 7 9 2524 M 5 10 17 2031 F 4 21 25 732 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 731 F 4 24 10 2033 M 2 19 23 822 M 1 14 23 1222 F 5 1 23 931 M 1 8 21 7;proc print;run;Output
Example:
DATA outdata;   INPUT age gender $ dept obs1 obs2 obs3;   CARDS; 24 F 1 19 25 731 F 4 24 10 2033 M 2 19 23 822 M 1 14 23 1222 F 5 1 23 931 M 1 8 21 7;proc print;run;
Output
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=', 'While using tab delimiter, the syntax would beinfile 'file-description' dlm='09'xWhile using colon delimiter, the syntax would beinfile 'file-description' dlm=':'Example:DATA outdata;    INFILE Datalines dlm =", ";   INPUT age gender $ dept obs1 obs2 obs3;    Datalines; 34, M, 1, 7, 7, 1431, F, 2, 2, 1, 2222, F, 5, 20, 5, 232, M, 4, 21, 8, 1841, M, 4, 7, 9, 2524, M, 5, 10, 17, 20;proc print;run;Output:
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=', '
While using tab delimiter, the syntax would beinfile 'file-description' dlm='09'xWhile using colon delimiter, the syntax would beinfile 'file-description' dlm=':'
While using tab delimiter, the syntax would beinfile 'file-description' dlm='09'x
infile 'file-description' dlm='09'x
While using colon delimiter, the syntax would beinfile 'file-description' dlm=':'
infile 'file-description' dlm=':'
Example:
DATA outdata;    INFILE Datalines dlm =", ";   INPUT age gender $ dept obs1 obs2 obs3;    Datalines; 34, M, 1, 7, 7, 1431, F, 2, 2, 1, 2222, F, 5, 20, 5, 232, M, 4, 21, 8, 1841, M, 4, 7, 9, 2524, M, 5, 10, 17, 20;proc print;run;
Output:
SAS Programming
Programming Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
All forms of formatted scanf() in C
Difference Between Imperative and Declarative Programming
Difference between while and do-while loop in C, C++, Java
Top 10 Programming Languages to Learn in 2022
Prolog | An Introduction
Header Guard in C++
Lists in Prolog
Scope of Variable in R
DataFrame Operations in R
Top 10 Fastest Programming Languages