Discover More Tips and Techniques on This Blog

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 capitalize first letter of the every word:/ PROPCASE FUNCTION:

How to use the SCAN function:

Concatenation functions in SAS 9.0:

SOUNDEX Function:

IFC and IFN functions:new IF functions & IF-THEN-ELSE vs SELECT:

How to remove the duplicate observations from the dataset using PROC SQL, DATASTEP/PROC SQL/or PROC SORT approaches ?

How can I count number of observations per subject in a data set?

How to save the log file or what is PROC PRINTTO procedure

How to calculate number of years and number of days between 2 dates;

How to customize page numbers in RTF output

How to create a comma separated file (.csv) of a SAS dataset?

Change all missing values of all variables into zeros/putting zeros in place of missing values for variables

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 variable. The easiest way to achieve this is:


cooked = round(raw, 10**-dp);

In this example, the required number of decimal places is set in the dataset variable dp. The ** operator is used to convert from a number of decimal places to a suitable rounding unit for the round() function: for example, when dp is set to 3, 10**-dp becomes 10**-3, which is 10-3 or 0.001.

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 dataset
Create a SAS dataset from raw data
List input
Column input
Formated input
Mixed input
Include data in program
Create permanent SAS dataset Working with SAS datasets,
Assignment statements
Functions
IF-THEN-ELSE, SELECT and LABEL statements
Subsetting data
DO-WHILE loop
DO-UNTIL loop Procedures
PROC PRINT and PROC SORT
PROC MEANS, PROC SORT and PROC PRINT
PROC FREQ
PROC UNIVARIATE
PROC REG
PROC GLM
PROC GPLOT


source: http://www.stattutorials.com/SASDATA/

o SAS Program Files
o PROCMEANS1.SAS
o PROCMEANS2.SAS
o PROCMEANS3.SAS
o PROCMEANS4.SAS
o PROCMEANS5.SAS (output)
o PROCUNI.SAS (univariate)
o PROCUNI2.SAS (advanced univariate)
o PROCUNI3.SAS (advanced univariate)
o PROCFREQ1.SAS (Frequency table)
o PROCFREQ2.SAS (Data from summarize counts)
o PROCFREQ3.SAS (Goodness of fit)
o PROCFREQ4.SAS (two-way table)
o PROCFREQ5.SAS (2x2 from summary data)
o PROCCORR1.SAS (correlation)
o PROCCORR2.SAS (matrix of scatterplots)
o PROCTTEST1.SAS (two-group t-test)
o PROCTTEST2.SAS (paired t-test)
o PROCANOVA1.SAS (One Way ANOVA, also PROC GLM)
o PROCGLM2.SAS (Repeated Measures ANOVA)
o PROCGLM2a.SAS (Repeated Measures ANOVA)
o PROC-LIFE-1.SAS (Survival Analysis PROC LIFETEST)
o BLAND-ALTMAN.SAS (Bland-Altman Analysis)

o ODS Examples
o ODS1.SAS (Example output without ODS)
o ODS2.SAS (Simple ODS invocation)
o ODS3.SAS (ODS using Science Style (RTF))
o ODS3A.SAS (Same ODS to HTML)
o ODS3B.SAS (Same to PDF)
o ODS4.SAS (t-Test ODS output)
o ODS5.SAS (Simple GCHART/ No ODS)
o ODS5A.SAS (Drill-down bar chart example)
o ODS6.SAS (2x2 Crosstab)
o ODS6A.SAS (2x2 Crosstab with TRACE)
o ODS6B.SAS (2x2 Crosstab/Selected tables)
o ODS7.SAS (Scatterlot matrix/Correlations)
o ODS8.SAS (Regression with ODS graphics output)
o ODS9.SAS (GLM with graphics output/boxplots)
o ODS10.SAS (Discover output names of components)
o ODS10A.SAS (Output ODS data to file)
o ODS10B.SAS (Merge and use ODS output data)
o SASLibrary.pdf (How to create a SAS Library -- required for some of the examples)


o SAS Data Files
o SOMEDATA.SAS7BDAT
o SBPDATA.SAS7BDAT
o LIFE.SAS7BDAT (for LIFETEST)

Here is the lsit  and brief description of available projects. Everyone should do the first 4 projects.


Project 1 An introduction to the SAS operating environment.

Project 2 The basic SAS data step with input of data directly through the cards statement; use of labels, the sort procedure and print procedure; the means procedure.

Project 3 Reading data from ASCII files; computing new variables in the data step; the means procedure.

Project 4 Modifying existing SAS data sets using set; using loops in the data step; the ttest procedure.

Project 5 Column-wise input; analysis of categorical data using chi-square tests.

Project 6 Updating existing SAS data sets with new data.

Project 7 Basics of presentation quality graphics with proc gplot and proc g3d.

Project 8 Basic one factor analysis of variance using proc GLM.

Project 9 Advanced analysis of variance, custom hypothesis tests, and other features of proc GLM.

Project 10 Multivariate analysis of variance using proc GLM.

Project 11 Basic Box-Jenkins modeling of univariate time series analysis using proc arima (time domain).

Project 12 Some aspects of frequency domain analysis of time series using proc spectra.

Project 13 Discriminant analysis with proc discrim.

Project 14 Reading data from dBase and DIF files; using dBase and DIF files instead of actual SAS datasets.

Project 15 Using arrays, first and last, and processing dates. Repeated measures analysis.

Source: http://javeeh.net

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=&IN OUT=&OUT &OPTN ;

by &VAR;

RUN;

%MEND SORT;

%SORT(IN=AE, AE1,USIBJID AEBODYSYS AESEV, NODUPKEY);







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
select INPUT (date,date9.)as ndate format=date9.
from date_char;
quit;
III) Using Arrays;

*using arrays to convert character date variables aestdtc and aeendtc into numeric variables:

data c_date;
set date;
array cha_date { 2 } $ 10 aeendtc aestdtc; array num_date { 2 } aeendt aestdt;
do i = 1 to 2;
num_date{ I } = input(cha_date{ i }, anydtdte10.);
end;
drop
aeendtc aestdtc;
run;


*Using Arrays converting variables from numeric to character;
data new; set old (rename=(b1=a1 b2=a2 b3=a3 b4=a4 b5=a5));
array aa $ b1 b2 b3 b4 b5;
array bb a1 a2 a3 a4 a5; do I=1 to dim(aa);
aa (i)= put(bb(i),8.0);
end;
drop I a1 a2 a3 a4 a5 a6; run;

*Using Arrays converting variables from character to numeric;
*By using the INPUT instead of PUT we can convert character variables into numeric;

data new ;
set old (rename=(b1=a1 b2=a2 b3=a3 b4=a4 b5=a5));
array aa b1 b2 b3 b4 b5;
array bb $ a1 a2 a3 a4 a5;
do I=1 to dim (aa);
aa (i)=input(bb(i),8.0);
end;
drop I a1 a2 a3 a4 a5 a6;
run;


Note: Dont forget to use $ sign inside the array statement when converting the vars from Num to char or Char to num.

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.