Advertisements

Create SAS Data Set with Raw Data Manually (3 Examples)

Sometimes, SAS users need to create SAS data set with raw data manually. Especially when users work on a small data set, they can create their own data set manually in SAS rather than importing one. In this section, we discuss on how to create a SAS data set with DATA step manually and also answer commonly asked questions. This post is a supplement to the SAS.support posts

Create SAS data set with data step, Create SAS data set from raw data manually, SAS data step, Data Step syntax in SAS, SAS data step examples

1. Data Step in SAS:

The SAS data step is used to create a SAS data set, which can be a data file or a SAS view. The data step uses input from raw data, remote access, assignment statements, or SAS data set. User use SAS Step to create their own data set when the data set is comparatively smaller. Let’s see examples below:

2. Create SAS data set manually:

To create the SAS data set manually, follow the codes:

/* To Create your first Dataset in SAS */

data student_grade;
input Name$ Math Stat Science;
datalines;
Sanam 90 95 94
Alex 84 85 88
Rich 92 93 94
Craig 88 78 72
Robert 72 74 68
run;

/*To print the Dataset you just created*/

proc print data=student_grade;
run;

After running the above code, then write the following code to check whether the table has been created:

SAS Data step to create data set manually

3.Rename a variable:

Sometimes, we need to change the name of variable (s). To change the name of the variable (s), the Generic Code is:

/* To Rename a variable, the Generic Code */

data data_set;
set new_data (rename=(Old_Name=New_Name));
run;

Example Code: We want to change the variable Name as Student_Name. To do so, the code is as follows:

Name of Variable we have: Name

Name we want: Student_Name

/* To Rename an existing variable */

data student; 
set student_grade (rename=(Name=Student_Name));
run;

/* Print the new dataset with the new name of variable */

proc print data=student;
run;

The result is as follows:

Chaning Name of variables in SAS. How to change names of variables in SAS. Rename old variable in SAS

4. How to Rename Multiple Variables in SAS

We want to change the names of the variables as follows:

Old Name New Name
Name Student_Name
Math Mathematics
Stat Statistics
Science Science2

The example code to rename the variables:

Advertisements
/* To Rename Multiple Variables */

data student; 
set student_grade (rename=(
Name=Student_Name
Math=Mathematics
Stat=Statistics
Science=Science2));
run;

/* Print the new dataset with the new name of variable */

proc print data=student;
run;

The Table with the New Names:

Rename multiple Variables in SAS

5. Create Table in SAS by PROC SQL

To create a table by inserting data manually, the code is as follows:

/* Step 1: Create a blank Table */

proc sql;
   create table student_grade
       (Name char(16),
        Math num,
        Stat num,
        Science num);

/* Insert Values in the Table */

insert into student_grade
    values('Sanam',90, 88, 95)
	values('Safa',92, 98, 100)
	values('Rich',88, 98, 75)
	values('Craig',87, 84, 85);

/* See all the variables */ 

	select * from student_grade;

The code and the table in SAS are as follows:

Create Table using SAS PROC SQL

6. SAS Infile Statement to import text file:

To import text file into SAS using infile statement, the example code is as follows:

/* To import the text file "grade1" into SAS */

data grade1; 
infile "C:\wikitekkee\Learning SAS\grade1.txt"; 
input Name$ Act Math Science;
run;

/* To print and check whether the dataset has been properly imported */

proc print data=grade1;
run;

Explanation:

Results: The result is as follows:

Infile Statement to import Text File into SAS

7. Import Excel File into SAS

To import Excel File into SAS, the code is as follows:

proc import out=grade
    datafile="C:\wikitekkee\Learning SAS\grade.xlsx"
    dbms=xlsx
    replace;
    getnames=YES;
run;

//To print and check whether the data set has been properly imported//

proc print data=grade;
run;

The example files: Excel to SAS

Import Excel into SAS

8. Import STATA File into SAS

To import STATA File into SAS, the example code is as follows:

/* To import STATA file into SAS */

proc import file="C:\wikitekkee\Learning SAS\grade2.dta"
out=work.grade2
dbms=DTA
replace;
run;

/* To print the data and check whether the data set has been properly imported into SAS */

proc print data=grade2;
run;

The Example Files: STATA to SAS

Import STATA File into SAS

9.  Import TEXT File into SAS

We have a data set “grade1.txt” located in the folder C:\wikitekkee\Learning SAS. We want to import the file into SAS. To do so, the code we use:

Advertisements
/* To import text file into SAS*/

proc import file="C:\wikitekkee\Learning SAS\grade1.txt"
    out=work.grade1
    dbms=tab
    replace;
run;

/* To print the data and check whether the data set has been properly imported into SAS */

proc print data=grade1 (obs=5) noobs;
run;

The Example Files: Text file into SAS:

Import Text File into SAS

We learned to Create SAS Data Set with Raw Data Manually as well as how to import dataset into SAS. Please comment, share, and ask your questions. Our team will try to answer your questions. Thank you for reading.

More related readings:

5 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Advertisements