Sas Programming



  1. Sas Programming 101
  2. Sas Programming Language
  3. Sas Programming For Beginners

SAS (Statistical analysis system) is one of the most popular software for data analysis. It is widely used for various purposes such as data management, data mining, report writing, statistical analysis, business modeling, applications development and data warehousing. Knowing SAS is an asset in many job markets. Free video tutorials that will teach you the basics of SAS programming and statistical analysis. A fun interactive community for SAS academic users, where you'll find forums, software support, instructional videos and more. A consistent user experience across all applications, whether you’re working on a class project or doing self-study. SAS offering free learning resources in celebration of programmers For more than 40 years, SAS programmers have crafted software and solutions that transform the world. From statistics to data science, to analytics and artificial intelligence, people writing code have architected a new economy with incredible opportunities. Statistical Analysis System (SAS) is a software suite that has been developed by SAS Institute, one of the leaders in analytics. It is useful for performing advanced analytics, multivariate analyses, business intelligence, data management functions, and also for conducting predictive analytics.

This module will show how to input raw data into SAS, showing how to read instream data and external raw data files using some common raw data formats. Section 3 shows how to read external raw data files on a PC, UNIX/AIX, and Macintosh, while sections 4-6 give examples showing how to read the external raw data files on a PC, however these examples are easily converted to work on UNIX/AIX or a Macintosh based on the examples shown in section 3.

1. Reading free formatted data instream

One of the most common ways to read data into SAS is by reading the data instream in a data step – that is, by typing the data directly into the syntax of your SAS program. This approach is good for relatively small datasets. Spaces are usually used to 'delimit' (or separate) free formatted data. For example:

After reading in the data with a data step, it is usually a good idea to print the first few cases of your dataset to check that things were read correctly.

Here is the output produced by the proc print statement above.

2. Reading fixed formatted data instream

Fixed formatted data can also be read instream. Usually, because there are no delimiters (such as spaces, commas, or tabs) to separate fixed formatted data, column definitions are required for every variable in the dataset. That is, you need to provide the beginning and ending column numbers for each variable. This also requires the data to be in the same columns for each case. For example, if we rearrange the cars data from above, we can read it as fixed formatted data:

The benefit of fixed formatted data is that you can fit more information on a line when you do not use delimiters such as spaces or commas.

Here is the output produced by the proc print statement above.

ProgrammingSas Programming

3. Reading fixed formatted data from an external file

Suppose you are using a PC and you have a file named cars3.dat, that is stored in the c:carsdata directory of your computer. Here’s what the data in the file cars3.dat look like:

Sas

To read the file cars3.dat, use the following syntax.

Here is the output produced by the proc print statement above.

Suppose you were working on UNIX. The UNIX version of this program, assuming the file cars3.dat is located in the directory ~/carsdata, would use the syntax shown below. (Note that the '~' in the UNIX pathname above refers to the user’s HOME directory. Hence, the directory called carsdata that is located in the users HOME directory.)

Likewise, suppose you were working on a Macintosh. The Macintosh version of this program, assuming cars3.dat is located on your hard drive (called Hard Drive) in a folder called carsdata would look like this.

In examples 4, 5 and 6 below, you can change the infile statement as these examples have shown to make the programs appropriate for UNIX or for the Macintosh.

Sas Programming 101

4. Reading free formatted (space delimited) data from an external file

Free formatted data that is space delimited can also be read from an external file. For example, suppose you have a space delimited file named cars4.dat, that is stored in the c:carsdata directory of your computer.

Here’s what the data in the file cars4.dat look like:

To read the data from cars4.dat into SAS, use the following syntax:

Here is the output produced by the proc print statement above.

5. Reading free formatted (comma delimited) data from an external file

Free formatted data that is comma delimited can also be read from an external file. For example, suppose you have a comma delimited file named cars5.dat, that is stored in the c:carsdata directory of your computer.

Here’s what the data in the file cars5.dat look like:

To read the data from cars5.dat into SAS, use the following syntax:

Here is the output produced by the proc print statement above.

Sas Programming Language

6. Reading free formatted (tab delimited) data from an external file

Free formatted data that is TAB delimited can also be read from an external file. For example, suppose you have a tab delimited file named cars6.dat, that is stored in the c:carsdata directory of your computer.

Here’s what the data in the file cars6.dat look like:

Sas Programming For Beginners

To read the data from cars6.dat into SAS, use the following syntax:

Sas

Here is the output produced by the proc print statement above.

7. Problems to look out for

  • If you read a file that is wider than 80 columns, you may need to use the lrecl= parameter on the infile statement.

8. For more information

  • For more detailed information on reading raw data into SAS, see Reading data into SAS in the SAS Library.
  • To learn how to create permanent SAS system files, see the Reading and writing SAS system files.
  • For information on creating and recoding variables once you have entered your data, see the SAS Learning Module on Creating and recoding variables.