Index to Statistics Tutorials
PROC MEANS Tutorial (Descriptive statistics)
PROC UNIVARIATE Tutorial (Distribution analysis)
New: PROC UNIVARIATE - Advanced Tutorial
PROC CORR Tutorial (Correlation)
PROC FREQ Tutorial 1 (Frequency Tables/Goodness of Fit)
PROC FREQ Tutorial 2 (Two-way tables)
PROC TTEST Tutorial (Two sample and paired t-tests)
New: A comparison of Paired & Independent Sample t-tests
PROC ANOVA & GLM Tutorial (One-Way ANOVA)
PROC GLM Tutorial (Repeated measures ANOVA using PROC GLM)
New: Survival Analysis & comparison of groups using PROC LIFEREG
Bland-Altman Analysis (Comparing two measures)
Inter-Rater Reliability, Kappa, Weighted Kappa (PROC FREQ)
New: SAS Functions (2-part tutorial)
Special SAS Topics
New: Read and Write SAS Data Sets the EASY way
Setting the SAS Initial Folder (default directory)
Using SAS ODS Output, Styles, Graphics, Data Data files and SAS code for tutorials
Welcome to StudySAS, your ultimate guide to clinical data management using SAS. We cover essential topics like SDTM, CDISC standards, and Define.XML, alongside advanced PROC SQL and SAS Macros techniques. Whether you're enhancing your programming efficiency or ensuring compliance with industry standards, StudySAS offers practical tips and insights to elevate your clinical research expertise. Join us and stay ahead in the evolving world of clinical data.
Discover More Tips and Techniques on This Blog
FIRST. and LAST. variables: Data step processing within by groups using the SET statement
FIRST. and LAST. variables: Data step processing within by groups
If you use a by statement along with a set statement in a data step then SAS creates two automatic variables, FIRST.variable and LAST.variable, where variable is the name of the by variable. FIRST.variable has a value 1 for the first observation in the by group and 0 for all other observations in the by group. LAST.variable has a value 1 for the last observation in the by group and 0 for all other observations in the by group.
The code shown below is available here.
data temp;
input group x;
cards;
1 23
1 34
1 .
1 45
2 78
2 92
2 45
2 89
2 34
2 76
3 31
4 23
4 12
;
run;
/**************************************************
The automatic variables first.group and last.group
are not saved with the data set. Here we write them
to data set variables to show their contents.
**************************************************/
data new;
set temp;
by group;
first=first.group;
last=last.group;
run;
proc print;
title 'Raw data along with first.group and last.group';
run;
/**************************************************
A common task in data cleaning is to identify
observations with a duplicate ID number. If we set
the data set by ID, then the observations which
are not duplicated will be both the first and the
last with that ID number. We can therefore write
any observations which are not both first.id and
last.id to a separate data set and examine them.
**************************************************/
data single dup;
set temp;
by group;
if first.group and last.group then output single;
else output dup;
run;
/**************************************************
We may also want to do data set processing within
each by group. In this example we construct the
cumulative sum of the variable X within each group.
**************************************************/
data cusum(keep=group sum);
set temp;
by group;
if first.group then sum=0;
sum+x;
if last.group then output;
run;
proc print data=cusum noobs;
title 'Sum of X within each group';
run;
/**************************************************
As an aside, if you simply want the sum of X within
each group, one of the many way of obtaining this
is with PROC PRINT.
**************************************************/
proc print data=temp;
title 'All data with X summed within each group';
by group;
sum x;
sumby group;
run;
source: http://www.pauldickman.com/teaching/sas/set_by.php
If you use a by statement along with a set statement in a data step then SAS creates two automatic variables, FIRST.variable and LAST.variable, where variable is the name of the by variable. FIRST.variable has a value 1 for the first observation in the by group and 0 for all other observations in the by group. LAST.variable has a value 1 for the last observation in the by group and 0 for all other observations in the by group.
The code shown below is available here.
data temp;
input group x;
cards;
1 23
1 34
1 .
1 45
2 78
2 92
2 45
2 89
2 34
2 76
3 31
4 23
4 12
;
run;
/**************************************************
The automatic variables first.group and last.group
are not saved with the data set. Here we write them
to data set variables to show their contents.
**************************************************/
data new;
set temp;
by group;
first=first.group;
last=last.group;
run;
proc print;
title 'Raw data along with first.group and last.group';
run;
/**************************************************
A common task in data cleaning is to identify
observations with a duplicate ID number. If we set
the data set by ID, then the observations which
are not duplicated will be both the first and the
last with that ID number. We can therefore write
any observations which are not both first.id and
last.id to a separate data set and examine them.
**************************************************/
data single dup;
set temp;
by group;
if first.group and last.group then output single;
else output dup;
run;
/**************************************************
We may also want to do data set processing within
each by group. In this example we construct the
cumulative sum of the variable X within each group.
**************************************************/
data cusum(keep=group sum);
set temp;
by group;
if first.group then sum=0;
sum+x;
if last.group then output;
run;
proc print data=cusum noobs;
title 'Sum of X within each group';
run;
/**************************************************
As an aside, if you simply want the sum of X within
each group, one of the many way of obtaining this
is with PROC PRINT.
**************************************************/
proc print data=temp;
title 'All data with X summed within each group';
by group;
sum x;
sumby group;
run;
source: http://www.pauldickman.com/teaching/sas/set_by.php
Subscribe to:
Posts (Atom)
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.