Discover More Tips and Techniques on This Blog

SAS® Programming Guidelines

This paper presents a set of programming guidelines and conventions that can be considered in developing code to ensure that it is clear, efficient, transferable and maintainable. These are not hard and fast rules but they are generally accepted good programming practices. These techniques can be used in all types of SAS® programs, on all platforms, by beginners and experts alike.



SAS ® PROGRAM EFFICIENCY FOR BEGINNERS


Guidelines for Coding of SAS® Programs

David Franklin's SAS Tips

2009

November - Looping the loop with DO loops
October - The Use and Abuse of the NODUPKEY option in the SORT Procedure
September - Comparing all datasets in one directory against all the datasets in a second dataset, using CALL EXECUTE
August - Selecting data for a particular month
July - I am missing a '0' in front of my day value, can SAS still read it okay? (or can SAS read a text date of '6JUN2009' correctly)
June - Prefixing a value with '+' or '-'
May - Why use dataset variable labels?
April - More on using a FORMAT to merge data
March - Delete all datasets in a library, except ...
February - Getting rid of the message "NOTE: BASE data set does not exist. DATA file is being copied to BASE file."
January - SUM(OF variables)


2008


December - Making a copy of a SAS dataset, Part 2
November - How would I set only character or numeric variables of an observation to missing?
October - Making a copy of a SAS dataset, Part 1
September - Character to Numeric Conversion
August - Numeric to Character Conversion
July - How would I set all variables of an observation to missing?
June - Making a SAS Transport File
May - Creating a Batch File
April - Transposing the data and label
March - Get a listing of all the SAS programs for a particular directory
February - Enclosing a negative value within brackets
January - Stripping (text)

2007

December - The CALL MISSING Routine
November - The MISSING function
October - Renaming a SAS dataset variable
September - Since when does a null string have a length of 1?
August - I just received a dataset with the dates in dd/mm/yy format but I want them in mm/dd/yy format for my report
July - The COLLATE function
June - PROC MEANS vs PROC SUMMARY
May - What SAS modules are on your system, and how to work around those that are missing
April - Removing labels and formats from all variables inside a dataset
March - Deleting old datasets using a date
February - The trig functions
January - FLOOR, CEIL, ROUND and INT

2006

December - Creating a graph using ODS RTF
November - Using the DHMS function to create a datetime value
October - Using CARDS or DATALINES to input data
September - FORMCHAR
August - Does the string contain a number?
July - LOWCASE/UPCASE/PROPCASE
June - Why use DATA _NULL_
May - Does x+y = SUM(x,y), revisited
April - Six ways to make your program run faster
March - SQL and the CALCULATED keyword
February - TRIM and TRIMN
January - Four Common Ways to Merge Two Datasets

2005

December - The CANCEL option in the RUN command
November - The DIM function for repeating the same action of different variables
October - The COMPARE function
September - Getting the last word in a string using SAS or Excel
August - Using SASHELP.VMACRO to view your macro variables, and %SYMDEL to delete them
July - Deleting a SAS dataset, revisited
June - Using PROC CHART to do a quick frequency analysis
May - The Variance results from SAS and Excel are different!
April - Finding a duplicate key record, Part II
March - The 2005 Conference Season begins
February - Sometimes, overwriting a dataset does not replace it
January - How does SAS store your date and/or time?

2004

December - Does x+y = SUM(x,y)?
November - An example of some unexpected results
October - Does a macro variable exist in your program?
September - Adding a value to an existing format
August - Common Mathematical Operators
July - Getting the background color from an Excel Worksheet cell
June - BYTE/RANK (this is not eating a hamburger)
May - The 2004 Conference Season begins
April - Getting a Cell Comment inside an Excel Worksheet
March - Finding a duplicate key record
February - FIRSTOBS=, OBS=
January - A BAT to create a Backup of your file

2003

December - SAS and Excel do not compute the same quartile value
November - Using a format to group basic statistical analysis
October - Coding a missing value
September - Sometimes your System Command Call within SAS does not work
August - COMPRESS or COMPBL, that is the question
July - Placing Unique values from a dataset into a macro variable
June - Share your datasets with others, consider the SASViewer
May - INDEX, INDEXW and INDEXC
April - Deleting a SAS dataset
March - Helping a SORT run faster
February - To avoid confusion, define the variables you create in a datastep
January - Introduction/Have a notebook handy

How to do a many-to-many merge in SAS without using SQL;

A many-to-many match-merge is more complicated. When we try to do many-to-many merge that means both the data sets have multiple occurrences of BY variable values. Simple match-merge the two data sets using the BY statement is never OK… Because when we do, we get the following note in the log.
"NOTE: MERGE statement has more than one data set with repeats of BY values."


SAS Programmer always tries to avoid this message in the log file….because this message means that the resulting data set is probably not what we expected.

Don’t be afraid that there is no way… you can do many-to-many merge using data step. A well known SAS Expert David Franklin points out a way to do many-to-many merge





Many Thanks David!

(Read his original post here)


NS8PO19P

The Fundamentals of MERGE

You can get the SAS Code back even If you haven’t saved it

If you happen to be one of the unlucky programmers who lost the SAS code … because you didn’t save it. There is solution for that…. SAS System automatically takes the backup of the SAS code for every 10 minutes (default);

Just look in the following location:

C:\Documents and Settings\Programmer Name\Application Data\SAS\EnhancedEditor\

Note: Replace “Programmer Name” with your login user id of the System you are using;
If you go the specified location above, you will see a copy of the unsaved version of the SAS code.
It will be quick if you search files with the extension name ‘.$AS’ (extension for auto-saved SAS codes)


If you want, you can also change the 10-minute time interval for Auto save…. Go to…


Tools Options Preferences Edit.


In the preferences dialog box, make sure to the change the time under Autosave every …. And click on OK ...

tod8. and picture formats: How Can I Find a Time Format that Does Not Remove the Leading Zero e.g. Time5

How Can I Find a Time Format that Does Not Remove the Leading Zero e.g. Time5
(output printed as 09:36 instead of 9:36)?
I have found an intersting solution to this problem in http://www.sas.com/; direct link

(Suggested by Stephen Hoar, Lloyds TSB)

There is currently no time format that puts leading zeros automatically in time values. But there are a number of ways of achieving this, below are some examples.

Example 1:
Create your own custom format using the following syntax. Then format your data values using the user defined format.


proc format ;
picture military other = '%0H:%0M:%0S' (datatype=time) ;
run ;

data test;
x= '9:36't ;
format x military8. ;
put x=;
run ;

x=09:36:00


Example 2:

You can also use the TODw.d format which does write leading zeros, but the original value must be a SAS datetime value. To convert your time values to datetime values use the DHMS function. In the DHMS function insert the value: 0 date, 0 hours, 0 minutes, and then the SAS time value as the number of seconds.

Then format the new variable with the TODw.d format and you will have the correct time, including the leading zeros.


data test;
x='9:36't;
y=dhms(0,0,0,x);
format y tod8.;
put y=;
run;

y=09:36:00

Have a look at the following Technical Support website for further examples of using Date & Time functions and formats: http://support.sas.com/techsup/sample/functions.html

SAS Arrays and DO-LOOPS

SAS Do Array

Three SAS Programs that use Arrays

SAS Macros that Convert a Directory of Transport Files

There is a set of SAS macros, converts a directory of transport files to a directory of SAS data sets and format catalogs (and vice versa). To see how to invoke the macros, look at the test following the last macro. The macros make the assumption that transport files created from data sets have the extension .xpt, and transport files created from format catalogs have the extension .xpf.
%expfmts : This macro will convert an existing format catalog into a data set in transport format.
%expdset: This macro will convert an existing SAS data set into a transport file.
%impfmts: This macro will convert a transport CNTLOUT data set into a native format catalog.
%impdset: This macro will convert a transport data set into a native SAS data set.
%getnames: This macro will create a SAS data set consisting of a variable called FILENAME. There will be one observation for each file in the specified directory with the specified extension.

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.