Read File on SAS – Part 1

There are several ways to access datafile on SAS. The easiest and simplest method is to copy and paste raw data on SAS editor. This only works on simple data for new users who need dataset to learn SAS and almost never happens in real life. Any new users to SAS can get sample data provided by SAS on this website.

https://support.sas.com/publishing/cert/sampdata.txt

You can just choose any suitable data and input on SAS editor & run using DATALINES statement. But first, we will need to identify a location to save our datalines- in this case we need to create a library known as “sasuser” using libname statement.

libname sas “I:\SAS\Sample Folders”;

The code above indicates:

  • libname => <statement>
  • sas => <library name>
  • “I:\SAS\Sample Folders” => <folder link where you have your library created to save the data input>

Once the library name has been created, we can then input the datalines by copying the statement below.

/* create data sets in SAS library using DATALINES*/

data sas.testdata;

   input ID $ 1-4 Name $ 6-19 Gender $ 20 Age 26-27 

   Salary 32-37;

   format Salary 7.2;

datalines;

0001 Jason,W       M    33    8000.00

0002 Alex,B        M    29    2000.00

0003 William,G     M    30    3000.00

0004 Cathy,G       F    36    5000.00

0005 Sally,H       F    34    4500.00



run;

The code above indicates:

  • /*create data sets in SAS library using DATALINES*/
    • <allow users to indicate any texts they want within /*text*/>
  • data sas.testdata;
    • <indicates data saved in library “sas” and dataset named “testdata”>
    • <always end with semicolon for SAS to know it has reached the end of the step>
  • input ID $ 1-4 Name $ 6-19 Gender $ 25 Age 30-31;
    • <start with input and then column names e.g. ID, Name, Gender>
    • <$ means we assign values to character variables>
    • <1-4 indicates we assign dedicated column positions from 1 to 4 for ID. It is also known as fixed-field data so each variable has its corresponding column positions>
    • <end with semicolon for SAS to know it has reached the end of the step>
  • format Salary 7.2;
    • <format Salary 7.2 simply means we assign a certain formatting to variable name “Salary”>
    • <this is a simple W.D format. There are also other formatting such as commaW.D and dollarW.D which we can discuss in another blog post>
    • <W.D means there is a total of W spaces including one space for decimal, and D spaces of decimal numbers>
    • <7.2 means 7 spaces of numbers including decimal and 2 spaces for no of decimals>
    • <end with semicolon for SAS to know it has reached the end of the step>
  • datalines;
    • <statement>
    • <end with semicolon for SAS to know it has reached the end of the step>
  • run;
    • <statement to execute all prior SAS statements>
    • <end with semicolon for SAS to know it has reached the end of the step>
Advertisements


Categories: SAS Learning

Tags: , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: