Posts

How to Read Delimited Text Files into SAS

This sample program shows you how to read a delimited text file into SAS. A text file is often referred as raw data that can be prepared in a variety of formats (e.g., csv [comma-separated values], tab-delimited, or spacedelimited). When reading text files with the DATA step, two statements are used: INFILE and INPUT statements. The INFILE statement is used to specify the physical file being read. You can use a FILENAME statement in conjunction with an INFILE statement (see Example.1). Or, you can specify the full path to the file in the INFILE statement (see example.2). A set of options are available in the INFILE statement: DLM, DSD, and LRECL. The DLM option allows you to tell SAS what character is used as the delimiter in the text file. If this option is not specified, SAS assumes the delimiter is a space. Common delimiters include commas, vertical pipes, semi-colons, and tabs. The DSD option has three functions when reading delimited files. The first function is to remove any quot...

How to Determine if a variable exists in the dataset or not

Programatically determine if a variable exists in a data set Use the OPEN and VARNUM functions to determine if a variable exists in a SAS data set. The OPEN function opens a SAS data set and returns a data set identifier. The VARNUM function returns the variable's position in the data set. If the variable does not exist then VARNUM returns a 0. /* Create sample data */ data test; input fruit $ count; datalines; apple 12 banana 4 coconut 5 date 7 eggs 9 fig 5 ; /* Specify the data set to open in the OPEN function. Specify the */ /* variable to be located in the second parameter of the VARNUM */ /* function. If the variable does not exist, the value of check */ /* will be zero. */ data _null_; dsid=open('test'); check=varnum(dsid,'count'); if check=0 then put 'Variable does not exist'; else put 'Variable is located in column ' check +(-1) '.'; run; OUTPUT: /* Partial LOG output */ 311 data _null_; 312 dsid=open('test'); 313 check=varnum...

What to do when we want only the even number observations

Output only even number observations. Note: The MOD function returns the remainder when values are divided. In this sample, when dividing i by 2, there will be no remainder for the even observations (2,4,6,8 and 10). If there is a remainder, the current observation has to be an odd numbered observation. This program outputs only the even numbered observations to a new data set. /* Create sample data set */ data one; do i=1 to 10; output; end; run; data two; set one; /* The MOD function returns the remainder from the division of */ /* argument-1 by argument-2. If the remainder is zero, when */ /* the second argument is 2, then the first argument must be */ /* even...therefore, output the observation. */ if mod(i,2)=0 then output; run; proc print; run; OUTPUT: Obs i 1 2 2 4 3 6 4 8 5 10 source:www.support.sas.com

How to verify the existence of the external file:

Verify the existence of an external file Conditionally execute code to read in a file only when the file exists. Note: Although your operating environment utilities may recognize partial physical filenames, you must always use fully qualified physical filenames with FILEEXIST. This example verifies the existence of an external file. If the file exists, read in the file using INFILE and INPUT statements. If the file does not exist, display a message in the SAS log that states the file does not exist. Note that in a macro statement you do not enclose character strings in quotation marks. /* If the file passed to the macro does exist, read in the file and create */ /* a character variable called VAR with a default length of 8 bytes. If */ /* file named in the macro call does not exist, write "FILE DOES NOT EXIST..." */ /* to the log. */ %macro in_file(file); %if %sysfunc(fileexist(&file))=1 %then %do; data a; infile "&file"; input var $; run; %end; %else %do; d...

Accurately Calculating Age with Only One Line of Code

direct link: This tip was written by William Kreuter , a senior computer specialist at the University of Washington in Seattle. He has used SAS software in public health research since 1981, and now specializes in manipulating large data sets for the School of Public Health and the School of Medicine. He can be reached at billyk@u.washington.edu. A frequent need of SAS software users is to determine a person's age, based on a given date and the person's birthday. Although no simple arithmetic expression can flawlessly return the age according to common usage, efficient code to do so can be written using SAS software's date functions. This article, by SAS software user William Kreuter, presents a one-line solution for this purpose. Put SAS Date Functions to Work for You Many kinds of work require the calculation of elapsed anniversaries. The most obvious application is finding a person's age on a given date. Others might include finding the number of years since any ...

Search a character expression for a string, specific character, or word:INDEX/INDEXC Functions

Image
Choose appropriate INDEX function to find target strings, individual letters, or strings on word boundaries. Note: Sample 1 uses INDEX to search for the first occurrence of a 'word' anywhere in a string. If the string is not found, the result is zero. Sample 2 uses INDEXC to locate the first occurence of any character specified in the excerpt. If no target is found, the result is zero.** Sample 3 uses INDEXW to find the target excerpt in a string on a word boundary. If the word is not found, the result is zero. RESULTS: Sample 1: INDEX Sample 2: INDEXC Sample 3: INDEXW source:www.support.sas.com

How to use the MISSING function when you don't know if the variable is characer or numeric

Image
Using the MISSING function from SAS Functions by Example By Ron Cody Ever need to check for a missing value, but you're not sure if the variable is character or numeric? No problem when you use the MISSING function. This function takes either character or numeric variables and it checks for the .A, .B, ._ numeric missing values as well. For example: DATA MISSING; INPUT CHAR $ X Y; IF MISSING (CHAR) THEN N_CHAR + 1; IF MISSING (X) THEN N_X + 1; IF MISSING (Y) THEN N_Y + 1; DATALINES ; CODY 5 6 . . . WHATLEY .A ._ LAST 10 20 ; PROC PRINT DATA=MISSING NOOBS ; TITLE " Listing of MISSING "; RUN ; A listing of MISSING , below, shows that the MISSING function works correctly with character and numeric values, including all the alternative numeric missing values: Listing of MISSING source: www.support.sas.com