Useful SAS Tips
These are useful SAS commands, procs, quirks, that are not often discussed but are requested/needed from time to time.
Problems with RUNNING PROC Export
Ran Proc Export in PC SAS and the log indicates there is an error involving the library not found or unable to connect???Apparently there is a software error in some HP printers which causes this error in PC SAS when the printer is set as the default.
The solution:
Set the default printer to a non HP or an HP printer that does not cause the error and make sure the printer setting for the session is not an offending HP printer on the Export proc.
Random Sample of Data
Example: I would like a random sample of PUMS data by household.
/****** selection of a random sample by household *****/ proc sort data=test; by sampleyr STATE SERIALNO; run; /* random choice by family */ proc surveyselect data=test out=rsfam method=SRS /* simple random sampling method */ n=1 /* sample size from each strata */; strata household /* variables in the stratification (& sorted by)*/; /*sampsize=1165 /* for each family */ run;
Code used multiple times
Sometimes code is used often in various parts of your program. Rather than copying the code to all the needed locations, use the LINK command to navigate to the code and back. In other programing languages this would be like using a subroutine. Here is an example of using LINK.
data test; input x; if x le 1 then link next; if x gt 4 then link next; else output; return; next: put 'The number in question is' x=; return; datalines; 1 4 2 0 6 run;
Arrays -
Data types
Seeking a type of data:
/* to correct a specific data type of variables */ data test; array no(*) _numeric_; do i= 1 to dim(no); if no(i)=-1 then no(i)=1; end; array alpha(*) _CHARACTER_; do i= 1 to dim(alpha); if alpha(i)='-1' then alpha(i)='1'; end; run;
/*******************************************************************************************/ /* the smt array nums w/ _numeric_ looks at all numeric variables & puts them into a array */ /* thus allowing a search for a specific condition with in the numeric variables */ /*******************************************************************************************/ data test; /* convert MSaccess checkbox values;*/ array nums _numeric_; do i = 1 to dim(nums); if nums(i) = -1 then nums(i) = 1; end; run;
Moving data around
/* Use of proc transpose for swapping data */ /* dob_by_yr is a dataset */ /* this makes a value into a variable name & makes 1 rec for all obs */ proc transpose data=dob_by_yr out=tdob; var COUNT; /* COUNT is the variable to be kept as a value */ id YEAR; /* the YEAR whose value becomes a variable */ by ID; /* sorted by variable called ID */ run; /* this takes a straight swap of location but the values are still values */ /* You get 2 recs 1 for each var */ proc transpose data=dob_by_yr out=tdob2; var YEAR COUNT; by ID; run;
Flagging errors in data
/* Create a date from the parts but flag erroneous/invalid dates */ data test; flag=0; input id mo day yr; date=mdy(mo,day,yr); /* creates a date */ if _error_ ne 0 then flag=1; datalines; 1 01 02 1966 2 06 15 1966 3 5 5 1967 4 09 31 1966 5 02 12 1967 6 8 13 1966 run;

