Posts

SAS Supplied Formats for SAS date 17750

Image

Replace Missing Numeric Values using Missing Option/Proc STDIZE

Image
MISSING OPTION Replacing missing values with the desired value like a zero is always a challenge, especially when we have a dataset with a number of columns to standardize. The OLD WAY of doing it to write a DATA step code with  if……then statements like...   if var=. then var =0; t o make 0 appear instead of . (dot) in the tables output. With the SAS option called missing you can save a lot of typing.  If you place the following SAS option code before the generation of the table output: Option missing=" 0 "; SAS will display 0 (zero) instead of the . (dot) on the table. In fact it can display whatever the character we would like to display for missing values.  You can use a - (line) or * (star). Always change it back the option missing setting to default as  Option Missing='';   otherwise you may endup getting unexpected results; Warning : Since we are using this option to display zeros instead of .(dots), you sh...

COMPRESS: SAS Function strips characters from the string

In SAS 9.1.3 , an extra argument ( MODIFIER ) is added to the SAS character string function COMPRESS and these modifiers modifies the action of the COMPRESS function; Syntax : COMPRESS ( <, chars><, modifiers>) ; Following characters can be used as modifiers. a – Compress or Delete all upper and lower case characters from String. ak - Compress or Delete alphabetic characters(1,2,3 etc) from String. kd- Compress or Delete characters(aplabets) from String.( Keeps only digits). d – Compress or Delete numerical values from String. i – Compress or Delete specified characters both upper and lower case from String. k – keeps the specified characters in the string instead of removing them. l – Compress or Delete lowercase characters from String. p – Compress or Delete Punctuation characters from String. s – Compress or delete spaces from String. This is default. u – Compress or Delete uppercase characters from String. See the Example below: data ...

MMDDYY+: Format that inserts a slash, space or "-" between the date, month and year

Image
Here is another new format which is available with SAS 9 version called MMDDYY+ . ( MMDDYY+ format can insert colons /slash or periods etc between date, month and year values); This new date format developed by SAS works for many other formats, for ex: MMDDYYB .; * format inserts Blanks in between date, month and years; MMDDYYC .; * format inserts Colons in between date, month and years; MMDDYYD.; * format inserts Dashes in between date, month and years; MMDDYYN .; * format inserts Nothing in between date,month & years; MMDDYYP .; * format inserts Periods in between date, month & years; MMDDYYS .; * format inserts Slash in between date, month and years; When you have date, month and year as separate variables and you want to insert colons or slash or periods and even blanks between them…. MMDDYY+ is your answer. Here I will show how? data have; input date month year; cards ; 25 1 2005 19 3 2006 12 2 2004 1 12 2007 ; run ; data want; set have...

ERROR: The format XXXX was not found or could not be loaded.

Image
Whenever you try to open a SAS data set which has permanent formats, you will get the error message in the log file saying “ERROR: The format XXXX was not found or could not be loaded.” This happens generally when you don't have the format library where the SAS dataset located. What happens here is, SAS will not permit you to open the data set without the format library. But, if you use options nofmterr; at the top of your program, SAS will opens the dataset for you despite the fact that it does not have the format library. SAS doesn't treat missing format or informat as an error when the options nofmterr; statement used. The FMTERR\NOFMTERR option determines whether or not SAS generates an error message when a variable's format cannot be found. ( source: support.sas.com) The caveat here is, you will not be able to see the formatted values for your variables, but you will be able to open or use the dataset. OPTIONS nofmterr; *you will not be able to see th...

How to check how the dataset got sorted without looking at the code?

Image
You can do this manually or programmatically. Manual Approach: Right click on the data set in the explorer and go to properties, then go to the details tab and see the sorted by values (Works only if you use Windows operating system) Programmatic approach: Use proc contents and create an output dataset of the contents. The sorted and sortedby columns will give us the details in about how the dataset got sorted in place. Note: proc contents will give missing values if the dataset isn’t sorted. *Example; proc sort data =sashelp.class out =class; by name sex age; run ; proc contents data =class out =contents noprint ; run ; proc print data =contents; var memname name sorted sortedby; run ; (' ’)

How to add leading zeros to numeric variables

Have you ever asked to create a variable with leading zeros? I mean 1 to 001 and 2 to 002. If you were, do you know how.... SAS has a special numeric format Zw.d . which can include leading zeros. Zw.d Format in which, w :   Width of variable. d :  Decimal Point. Zw.d format writes standard numeric data with leading 0's.  The Z w . d  format is similar to the  w.d  format except that Z w . d  pads right-aligned output with 0s instead of blanks.  (Ref: SAS 9.2 Language Reference : Dictionary, 4th edition); Let me give you a scenario where this can be very useful: You have a variable called site with the numbers as 101, 999, 1001 and 1200. If you want to create a USUBJID variable with fixed length for all the subjects, we need to modify the site variable variables to fixed length. (length=4). You can do that using Z4.d format. Note: As this is a numeric format, this can be applied to numeric variables only. *z4.format is used to a...