Posts

SAS sample programs

SAS sample code programs: Macro for sorting the variables: How to convert character date values into numeric date values using DATASTEP/PROC SQL and ARRAYS: How to detect missing values using Arrays: First. & Last. Variables: How to determine the last observation in the dataset: How to determine whether a numeric or character value exists within a group of variables: Lag Function: How to obtain information from the previous observation: How to create a new dataset from multiple datasets based on the sorted order: Dynamically generate SET statement to combine multiple datasets: How to determine which dataset contributed an observation: How to determine if a variable exists in a dataset or not: How to Read Delimited Text Files into SAS: How to keep only even number observations in the dataset: How to verify the existence of an external file: Accurately calculating age in one line code: How to use INDEX/INDEXC functions: Finding the number of observations in the dataset: How to capita...

BASE SAS CERTIFICATION SUMMARY FUNCTIONS

Certification Summary---Functions: sourec: http://wiki.binghamton.edu/index.php/Certification_Summary---Functions SAS Functions can be used to convert data and to manipulate the values of character variables. Functions are written by specifying the function name, then it's arguments in parentheses. Arguments can include variables, constants, or expressions. Although arguments are typically separated by commas, they can also be specified as variable lists or arrays. Contents[ hide ] 1 YEAR, QTR, MONTH and DAY Functions 2 WEEKDAY Function 3 MDY Function 4 DATE and TODAY Function 5 SUBSTR Function 6 INDEX Function 7 UPCASE Function 8 LOWCASE Function 9 INT Function 10 ROUND Function 11 TRIM Function 12 Some Sample Certification Examples 13 Points to remember SAS Tip: How to round the numbers: It's often a requirement to round the values of one variable to a precision defined in a different variable - either another data set variable, or a macro ...

SAS sample programs

Reading/Writing Files Making a fixed format file Making a SAS Cport file Reading a SAS Cport file Reading multiple raw data files, Version 8 Reading multiple raw data files (version 6.x) Using a SAS macro to "set" multiple files Other Imputing the median Checking for duplicate Ids Macro to compute a rolling standard deviation Changing the length of a character variable Replacing strings Concatenating string variables using CAT functions Simple macro to do repeated procs Eliminate useless variables Matching husbands and wives Creating a wide table from a long dataset using PROC TABULATE How can I "fill down" a variable? Creating a long list of variable names based on an abbreviated one Filling in Missing Values Over Time Dummy Coding a Categorical Variable Using a Macro Program A few SAS macro programs for renaming variables dynamically source: http://oregonstate.edu/dept/statistics/sasclass/examples.htm Creating SAS datasets Read a SAS da...

macro for sorting the the datasets

MACRO FOR SORTING: Rather than using the Proc Sort procedure all the time..... you can just use the following macro.... and call it when you req... to sort any SAS dataset..... EXAMPLE1: %macro srt(dtn,keyvar); proc sort data =&dtn; by &keyvar; run; %mend srt; %srt (ie,PT IEORRES); *the above step will tell SAS to sort the IE dataset with the by variables PT and IEORRES respectively. EXAMPLE2: *Sometimes we need to create an output dataset during the sorting process i.e in the Proc sort step in such a case use the below macro to do the same; %MACRO SORT(IN=,VAR=,OUT=); PROC SORT DATA=&IN OUT=&OUT; by &VAR; RUN; %MEND SORT; %SORT (IN=CEC1,VAR=PT,OUT=CEC2); %SORT (IN=DERIVE.HEADER,VAR=PT,OUT=HEADER1); EXAMPLE3: *Sometimes we need to use the NODUPKEY option to delete the duplicate observations in the dataset in such a case use the below macro to do the same; % MACRO SORT(IN,VAR,OUT,OPTN); PROC SORT DATA =...

How to convert numeric date values into character and from character date values into numeric using DATASTEP, PROC SQL and ARRAYS

1) Converting character date values into numeric: /*I) Using the DATASTEP:*/ 1) Data dates; input cdate $9.; cards; 16-apr-07 01-01-07 02-jun-07 13-sep-07 ; run; Data Convert; set dates; Date = input ( cdate , ANYDTDTE9.); format date date7.; run; 2) Data dates ; input cdate $9.; cards; 16-apr-2007 01-01-2007 02-jun-2007 13-sep-2007 ; run; Data Convert ; set dates; Date = input ( cdate , ANYDTDTE11.); format date date9.; run ; *II) Using Proc SQL; *Numeric date variable can be converted to character date variable by using the PUT function within PROC SQL.; proc sql; create table date_char as select PUT(date, date9.) as ndate from date_num; quit; *Character date variable can be converted to numeric date variable by using the INPUT function within PROC SQL.; Proc sql; create table date_num as select INPUT (date, mmddyy10.) as ndate format= mmddyy10. from date_char; quit ; Or Proc sql ; create table date_num as sel...

12 Ways to save SAS data

12 Ways to save SAS data source/direct link: http://wiki.binghamton.edu/index.php/12_Ways_to_save_SAS_data 1 Using CARDS; File Save (with editor window active) 1.1 To get it back 2 Cut and Paste (ex. Fixed format data) 2.1 To Get Them Back (space-delimited or fixed-formatted text) 2.2 Links 3 LIBNAME 3.1 To get it (them) back 4 Explorer with one LIBNAME statement?, menu/New Library button, Library Icon 4.1 To get it back 5 DATA ' '; direct access (including Unix ex.) 5.1 To get it back 5.2 Links: 6 In Excel Save As .XLS 7 In Excel Save As CSV (move to Unix, SSH) 7.1 To get it back(in SAS for Windows) 7.2 To Move (Upload) the CSV File from Windows to Unix 7.3 To get the data back (on Unix) 7.4 Links 8 In SPSS Save as 8.1 To get it (our data) back 9 In SAS Save as XPT file 10 XML 11 LIBNAME sasengine 12 DBF (to be written) 12.1 Saving a DBF (from Access?) 12.2 To get it back 13 13 ?PROC DATASOURCE?, ?Stata, ?S-Plus (to be written)

Getting the Data into SAS

GETTING DATA INTO SAS direct link: http://www.biostat.ucla.edu/course/m403b/M403B2005_2DataInput.pdf 1. Importing data from other sources • Use DBMS Copy, STAT Transfer, or some other software that performs similar functions. • Use SAS/ACCESS or the SAS Import feature in Windows. • Use PROC IMPORTS to read certain types of data files. • Create a raw data (ASCII) file then use INPUT and INFILE statements to read the raw data file in SAS programs. 2. Creating SAS data sets with raw (ASCII) data files • Data are placed within the SAS program directly after the DATALINES or CARDS statement. Use INPUT statement to read the data. *** Read data within SAS program as SAS data file ***; DATA survey; INPUT Q1Major Q2Degree Q3SASpast Q4ProjData Q5SASexprn Q6SASfutur; DATALINES; 2 1 1 1 2 8 2 2 0 0 0 7 2 1 0 0 1 4 2 1 0 1 0 8 more raw data lines ; RUN ; • In the above example, – Spaces (or other types of delimiters) are required between data values – Missing values must be specified (blank sp...