Get overview of distinct values in dataset

The code below will make distinct values of all the variables in a dataset and list them besides each other for a better overview.

Copy .sas files (program files) with SAS

The code below copies all the SAS-programs (*.sas) files in a directory to another directory. This solution should be used if you don’t want to use an OS-command that copies the files. Using an OS-command is a lot easier and doesn’t require as much code. But of course depends on the OS your running on. This solution is OS independent.


Execute OS-command

The code below executes a OS-command in a nice and easy fashion. It also returns the returncode (rc) for the execution of the command in the OS-environment.


Commenting in SAS

I think the best way to start a SAS-program is to do a comment as described by the template below.

Comments should also be done above each datastep or procedure, And changes to the programs should be contained in a versioning system eg like Subversion (SVN).If you do not have a versioning system, then I think the comments should be something like the comments below.


Using SAS display manager (DM) for data exploration

The display manager (also known as DM) in base-SAS can be used for data exploration. For this demonstration the dataset sashelp.class will be used. Now sashelp.class is easy to get an overview of because it only has five variables. But if you have a lot of variables then some kind of data exploration/data manipulation might be handy.

Let’s say that you would like to take a look at the varable Age. In the DM you will write keep age.


This will result in a displaying of the data only showing the variable age. Now you would like to have all the other variables shown but keep age as the first variable being displayed. This can be done writing the command unide _all_ in the DM.


Now all the variable will be shown with age as the first variable.


It is also possible to keep multiple variables. In the DM you can eg. write keep ‘height weight’. Remember that when keeping multiple variables you will have to write the variables in ‘ ‘.

Now only these variables will be shown.

You can again unhide the rest of the variables writing unhide _all_ this will keep the height and weight variables as the first variables being shown.

Get SQL recipe for a dataset in SAS

The code below will make a file class.sql containing the SQL-code for creating the dataset sashelp.class

The file will look something like this

Create table CLASS
(Name varchar(7), Sex varchar(1), Age float, Height float, Weight float);
Insert into CLASS(Name, Sex, Age, Height, Weight)
Values (‘Alfred’, ‘M’, 14, 69.0, 112.5);
Insert into CLASS(Name, Sex, Age, Height, Weight)
Values (‘Alice’, ‘F’, 13, 56.5, 84.0…

Making an empty dataset in SAS

The code below shows you how to make an empty dataset in SAS.
If you omit the if-sentence  and below, then you will get an empty row in the dataset.

This can also be done a bit easier in SQL.

Comparing datasets in SAS

The code below compares two datasets. It merges them together and makes three datasets. One dataset contains identical observations from the two datasets. The second dataset contains observations only found in one dataset. And the third dataset contains observations only found in the other dataset.