Even though there is no function is available in SAS to do exactly the opposite work of the LAG function (i.e: reading the next record while working on the current one), there are few things you can do to do exactly that.
Here are few simple techniques which are proved to work without any problem.
*SAMPLE DATASET;
data test;
input id age grp ;
datalines;
1 10 1
2 20 1
3 30 1
4 40 1
5 50 1
1 10 2
2 20 2
3 30 2
4 40 2
5 50 2
;
run;
*1) Using the POINT feature along with automatic variable _N_;
This solution was suggested by Paul M. Dorfman;
data leads;
_n_ ++ 1;
if _n_ le n then do;
set one point=_n_;
leadage=age;
end;
set one nobs=n;
run;
*OR*;
data leads;
_n_ ++ _n_ lt n;
set one point=_n_;
leadage=age;
set one nobs=n end=end;
if end then leadage=.;
run;
By using the above techniques, you can jump a bit higher and even look values of two, three or any numbers of observations in advance by advancing the value of automatic variable _N_ by appropriate number (in general _N_ ++ n ). Only thing you have to keep in mind while doing so is before applying the POINT processing, you have to apply appropriate lag (lagn) to grouping variable.
References:
SAS-L. http://www.listserv.uga.edu/cgi-bin/wa?A2=ind9904E&L=sas-l&P=R6185
http://www.nesug.org/Proceedings/nesug09/cc/cc26.pdf
Option3:
option mergeNoBy=nowarn; *Supress the warning message in the log. WARNING: No BY statement was specified for a MERGE statement.;
data lead;
merge one one(firstobs = 2 rename=(age=leadage));
run;
Sunday, February 6, 2011
Saturday, January 22, 2011
STUDY 'DAY' CLCULATION (ONE-LINER)
Recently I stumbled upon a SUGI-Paper SAS 1-Liners by Stephen Hunt. I liked the way Stephen developed the 1-liner for STUDY DAY calculation.
When it comes to calculating such, some programmers opt to both with evaluating whether a visit date occurred on or /after randomization.
if visdt > randdt >.z then stydy=visdt-randdt;
else stydt=visdt-randdt+1;
However, this is unnecessary, sine the 1-liner will suffice:
if visdt >.z & randdt >.z then stydy=visdt-randdt+(visdt>=randdt);
Thanks to Stephen for his code.
Here is the code I use from now to compute the study day.
if nmiss(visidt,randdt)=0 then stydy=visdt-randdt+(visdt>=randdt);
One of the most common calculation used across all types of programming is determining a relative 'day' based on 2 date fields. In clinical trials the initial 'Study Day' is generally considered to begin at either randamization or dosing, thus assessments made prior to this starting point require a slight variation in the calculating in order to preserve the typical 'no day 0' concept.
SUGI proceedings10/054-2010.pdf |
if visdt > randdt >.z then stydy=visdt-randdt;
else stydt=visdt-randdt+1;
However, this is unnecessary, sine the 1-liner will suffice:
if visdt >.z & randdt >.z then stydy=visdt-randdt+(visdt>=randdt);
Thanks to Stephen for his code.
Here is the code I use from now to compute the study day.
if nmiss(visidt,randdt)=0 then stydy=visdt-randdt+(visdt>=randdt);
Sunday, December 5, 2010
Easy way to UPCASE variable names of SAS dataset
option VALIDVARNAME=UPCASE;
Use trhe above option statement to upcase the variable name of the SAS dataset irrespective of type of variable in the dataset (character or numeric).
The following example shows how the option sattement VALIDVARNAME=UPCASE works.
proc contents data=sashelp.class out=test;
run;
Note: Propcase variable names.
*Upcasing the variables;
option validvarname=upcase;
proc sort data=sashelp.class out=test; run;
Because of the option statement. Ex: 'Age' becomes 'AGE' and 'Height' becomes 'HEIGHT' etc.
See the SAS Language Reference dictionary to get more details.
Another way to do this is to use a macro and I call it as UPCASE macro.
proc sort data=sashelp.class out=test;
by name;
run;
%macro upcase (lib,dsn);
*Create a macro variable with the total number of variable count;
data _null_;
set sashelp.vtable(where=(libname="&LIB" and memname="&DSN"));
call symput('nvars',nvar);
run;
*Create a macro variable with variable names;data _null_;
set sashelp.vcolumn(where=(libname="&LIB" and memname="&DSN"));
call symput(cats("var",_n_),name);
run;
proc datasets library=&LIB;
modify &DSN;
%do i = 1 %to &nvars;
rename &&var&i=%upcase(&&var&i);
%end;
;
quit;
run;
%mend;
%upcase(WORK,TEST);
Use trhe above option statement to upcase the variable name of the SAS dataset irrespective of type of variable in the dataset (character or numeric).
The following example shows how the option sattement VALIDVARNAME=UPCASE works.
proc contents data=sashelp.class out=test;
run;
Note: Propcase variable names.
*Upcasing the variables;
option validvarname=upcase;
proc sort data=sashelp.class out=test; run;
Because of the option statement. Ex: 'Age' becomes 'AGE' and 'Height' becomes 'HEIGHT' etc.
See the SAS Language Reference dictionary to get more details.
Another way to do this is to use a macro and I call it as UPCASE macro.
proc sort data=sashelp.class out=test;
by name;
run;
%macro upcase (lib,dsn);
*Create a macro variable with the total number of variable count;
data _null_;
set sashelp.vtable(where=(libname="&LIB" and memname="&DSN"));
call symput('nvars',nvar);
run;
*Create a macro variable with variable names;data _null_;
set sashelp.vcolumn(where=(libname="&LIB" and memname="&DSN"));
call symput(cats("var",_n_),name);
run;
proc datasets library=&LIB;
modify &DSN;
%do i = 1 %to &nvars;
rename &&var&i=%upcase(&&var&i);
%end;
;
quit;
run;
%mend;
%upcase(WORK,TEST);
Thursday, November 4, 2010
MDY Function
The MDY function converts MONTH, DAY, and YEAR values to a SAS date value. For example, MDY(10,19,1999) returns the SAS date value '19OCT99'D.
Syntax: MDY(month,day,year)
Arguments
month : specifies a numeric expression that represents an integer from 1 through 12.
day :specifies a numeric expression that represents an integer from 1 through 31.
year :specifies a two-digit or four-digit integer that represents the year. The YEARCUTOFF= system option defines the year value for two-digit dates.
If you know month, day, and year values, it’s very easy to derive date variable. You just need to use MDY function (of course, month, day, and year should be numeric). However, if the data is character then the conversion to numeric should occur first and then the conversion to the date value.
Example:
*When month, day, and year has numeric values;
data test;
year=1999;
month=12;
day=19;
newdate=mdy(month,day,year);
format newdate yymmdd10.;
run;
*When month, day, and year has character values;
data test;
year='1999';
month='12';
day='19';
newdate=mdy(input(month,2.),input(day,2.),input(year,4.);
format newdate yymmdd10.;
run;
You can use CATX and Input functions to get the same result.
newdate=input(catx('-',year,month,day),yymmdd10.);
Ref: SAS Help and Documentation.
Syntax: MDY(month,day,year)
Arguments
month : specifies a numeric expression that represents an integer from 1 through 12.
day :specifies a numeric expression that represents an integer from 1 through 31.
year :specifies a two-digit or four-digit integer that represents the year. The YEARCUTOFF= system option defines the year value for two-digit dates.
If you know month, day, and year values, it’s very easy to derive date variable. You just need to use MDY function (of course, month, day, and year should be numeric). However, if the data is character then the conversion to numeric should occur first and then the conversion to the date value.
Example:
*When month, day, and year has numeric values;
data test;
year=1999;
month=12;
day=19;
newdate=mdy(month,day,year);
format newdate yymmdd10.;
run;
*When month, day, and year has character values;
data test;
year='1999';
month='12';
day='19';
newdate=mdy(input(month,2.),input(day,2.),input(year,4.);
format newdate yymmdd10.;
run;
You can use CATX and Input functions to get the same result.
newdate=input(catx('-',year,month,day),yymmdd10.);
Ref: SAS Help and Documentation.
Monday, October 4, 2010
Overview on CDISC Implementation
CDISC Advantages
CDISC has developed a set of data standards to enhance
data collection,
management,
analysis, and
reporting efficiencies,
improve safety monitoring, and
streamline the review and approval process for investigational treatments.
Under the ICH’s electronic Common Technical Document (eCTD) guidance, CDISC Study Data Tabulation Model (SDTM) is the preferred standard for content format and structure of clinical data for all clinical studies. Based on proposed federal regulations, the FDA will mandate that all clinical trial submissions be in electronic format and that the content comply with data standards guidance. Veristat helps our clients by not only implementing these standards on a project or program, but also by providing our clients with an understanding of the CDISC standards.
source:veristatinc.com
('’)
Thursday, September 16, 2010
How to convert the datetime character string to SAS datetime value? (ANYDTDTM and MDYAMPM formats)
When we have a string like this "9/01/2010 11:52:54 AM" and would like to translate the string to a numeric SAS date time variable, most of the times we use SCAN function to extract the information to get the DATETIME format. This is definitely a tedious job.
SAS formats (MDYAMPM, ANTDTDTM) comes to rescue us. Here is how it works.
data test;
length date $25;
date="9/01/2010 11:52:54 AM";
*Convert the character string to SAS datetime value;
datetimevar =input(date,mdyampm25.2);
datetimevar1 =input(date,anydtdtm20.);
*Apply format to the SAS date time value;
format datetimevar datetimevar1 datetime19.;
run;
Result: 01SEP2010:11:52:54
*ANYDTDTM and MDYAMPM informats work together when the datetime value has AM PM specified or day, month, and year components are not ambiguous.
The MDYAMPMw. format writes datetime values with separators in the form mm/dd/yy hh:mm AM PM, and requires a space between the date and the time.
The ANYDTDTM w. format writes datetime values with separators in the form dd/mm/yy hh:mm AM PM, and requires a space between the date and the time.
When a value is read with ANYDTDTMw. informat and the style of the value is dd/dd/dd(dd) tt:tt:tt AM
PM, the MDYAMPMw.d informat is called to read the value. If the AM PM component is not present, the MDYAMPMw.d informat is used as long as the month and day components aren't ambiguous. If they are ambiguous, the value of the DATESTYLE= system option is used to determine the order of month, day, and year components.
MDYAMPMw.d Format
_______________________________________
Writes datetime values in the form mm/dd/yy hh:mm AM PM . The year can be either two or four digits.
Details
The MDYAMPMw.d format writes SAS datetime values in the following form:
mm/dd/yy hh:mm
The following list explains the datetime variables:
mm is an integer from 1 through 12 that represents the month.
dd is an integer from 1 through 31 that represents the day of the month.
yy or yyyy specifies a two-digit or four-digit integer that represents the year.
hh is the number of hours that range from 0 through 23.
mm is the number of minutes that range from 00 through 59.
AM PM specifies either the time period 00:01-12:00 noon (AM) or the time period 12:01-12:00 midnight (PM). The default is AM.
date and time separator characters is one of several special characters, such as the slash (/), colon (:), or a blank character that SAS uses to separate date and time components.
Source: SAS(R) 9.2 Language Reference: Dictionary, Third Edition
Examples:
These examples illustrate how the ANYDTDTMw. informat reads values based upon an AM PM specification and the DATESTYLE= system option.
--------------------------------------------------------------------------------
/* Since AM is specified with the value, the ANYDTDTM informat is called to read the datetime value. */
options datestyle=dmy;
data test1;
format xtext $22. xdate DATETIME18.;
xtext="07/01/2008 12:30:00 AM";
xdate=input(xtext,ANYDTDTM30.);
proc print;
run;
/* Since AM PM aren't specified and the month and day components are ambiguous, the DATESTYLE= system option is used to determine their order. */
options datestyle=dmy;
data test2;
format xtext $22. xdate DATETIME18.;
xtext="07/01/2008 12:30:00";
xdate=input(xtext,ANYDTDTM30.);
proc print;
run;
Source: http://support.sas.com/kb/37/309.html
If you have a date "Saturday, November 01, 2008" and would like to convert it to numeric with DDMMYY10. format here is the way to do it.
data fmtchnge;length worddate $40;
worddate = "Saturday, November 01, 2008";
dmyfmt= input(substr(worddate,findc(worddate,',')+1),anydtdte30.);
*dmyfmt=input(substr(worddate,index(worddate,',')+2),anydtdte32.);
format dmyfmt mmddyy10.;
run;
('’)
SAS formats (MDYAMPM, ANTDTDTM) comes to rescue us. Here is how it works.
data test;
length date $25;
date="9/01/2010 11:52:54 AM";
*Convert the character string to SAS datetime value;
datetimevar =input(date,mdyampm25.2);
datetimevar1 =input(date,anydtdtm20.);
*Apply format to the SAS date time value;
format datetimevar datetimevar1 datetime19.;
run;
Result: 01SEP2010:11:52:54
*ANYDTDTM and MDYAMPM informats work together when the datetime value has AM PM specified or day, month, and year components are not ambiguous.
The MDYAMPMw. format writes datetime values with separators in the form mm/dd/yy
The ANYDTDTM w. format writes datetime values with separators in the form dd/mm/yy
When a value is read with ANYDTDTMw. informat and the style of the value is dd/dd/dd(dd) tt:tt:tt AM
PM, the MDYAMPMw.d informat is called to read the value. If the AM PM component is not present, the MDYAMPMw.d informat is used as long as the month and day components aren't ambiguous. If they are ambiguous, the value of the DATESTYLE= system option is used to determine the order of month, day, and year components.
MDYAMPMw.d Format
_______________________________________
Writes datetime values in the form mm/dd/yy
Details
The MDYAMPMw.d format writes SAS datetime values in the following form:
mm/dd/yy
The following list explains the datetime variables:
mm is an integer from 1 through 12 that represents the month.
dd is an integer from 1 through 31 that represents the day of the month.
yy or yyyy specifies a two-digit or four-digit integer that represents the year.
hh is the number of hours that range from 0 through 23.
mm is the number of minutes that range from 00 through 59.
AM PM specifies either the time period 00:01-12:00 noon (AM) or the time period 12:01-12:00 midnight (PM). The default is AM.
date and time separator characters is one of several special characters, such as the slash (/), colon (:), or a blank character that SAS uses to separate date and time components.
Source: SAS(R) 9.2 Language Reference: Dictionary, Third Edition
Examples:
These examples illustrate how the ANYDTDTMw. informat reads values based upon an AM PM specification and the DATESTYLE= system option.
--------------------------------------------------------------------------------
/* Since AM is specified with the value, the ANYDTDTM informat is called to read the datetime value. */
options datestyle=dmy;
data test1;
format xtext $22. xdate DATETIME18.;
xtext="07/01/2008 12:30:00 AM";
xdate=input(xtext,ANYDTDTM30.);
proc print;
run;
/* Since AM PM aren't specified and the month and day components are ambiguous, the DATESTYLE= system option is used to determine their order. */
options datestyle=dmy;
data test2;
format xtext $22. xdate DATETIME18.;
xtext="07/01/2008 12:30:00";
xdate=input(xtext,ANYDTDTM30.);
proc print;
run;
Source: http://support.sas.com/kb/37/309.html
If you have a date "Saturday, November 01, 2008" and would like to convert it to numeric with DDMMYY10. format here is the way to do it.
data fmtchnge;length worddate $40;
worddate = "Saturday, November 01, 2008";
dmyfmt= input(substr(worddate,findc(worddate,',')+1),anydtdte30.);
*dmyfmt=input(substr(worddate,index(worddate,',')+2),anydtdte32.);
format dmyfmt mmddyy10.;
run;
('’)
Sunday, September 12, 2010
SAS Keyboard Shortcuts
Here are the few shortcuts you need to know to speed up the code writing. These work in both EPG (Enterprise Guide) and SAS Enhanced Editor.
Shortcuts and their descriptions:
Remember that the keyboard shortcuts listed here are default.
Selection Operations:1) Comment the section with line comments (/): press CTL + /
2) Undo the comment: press CTL + SHIFT + /
3) Convert selected text to lowercase: press CTL + SHIFT + L
4) Convert selected text to uppercase: press CTL + SHIFT + U
Shortcuts (pre-defined) CTRL+Shift+L or +U (only for the enhanced editor), which convert all selected text into lowercase or uppercase respectively. These become very handy when we insert the text by copy+paste.
5) Indent selected section: press TAB
6) Un-indent selected section: press SHIFT + TAB
7) To move curser to the matching DO/END statement: press
ALT + [ or
ALT + { or
ALT+] or
ALT + }
Miscellaneous:1) To see the desktop: press (Window’s Key + M) or (Window’s Key + D)
Navigate around Text:
1) Move to beginning of line: Press Home
2) Move to top: Press CTRL+Home
3) Move to end: Press CTRL+End
Mark (Highlight) the Text:
1) Mark to beginning of line: Press SHIFT+Home
2) Mark to end of line: Press SHIFT+End
3) Mark to top: Press SHIFT+CTRL+Home
4) Mark to end: Press SHIFT+CTRL+End
Window Control:
1) To cascade the windows: Press SHIFT+F5
2) To Tile the windows: Press SHIFT+F4
3) To go to the next window: Press CTRL+F6
4) To close the active window: Press CTRL+F4
5) To exit the SAS system: Press ALT+F4
('’)
Shortcuts and their descriptions:
Remember that the keyboard shortcuts listed here are default.
Selection Operations:1) Comment the section with line comments (/): press CTL + /
2) Undo the comment: press CTL + SHIFT + /
3) Convert selected text to lowercase: press CTL + SHIFT + L
4) Convert selected text to uppercase: press CTL + SHIFT + U
Shortcuts (pre-defined) CTRL+Shift+L or +U (only for the enhanced editor), which convert all selected text into lowercase or uppercase respectively. These become very handy when we insert the text by copy+paste.
5) Indent selected section: press TAB
6) Un-indent selected section: press SHIFT + TAB
7) To move curser to the matching DO/END statement: press
ALT + [ or
ALT + { or
ALT+] or
ALT + }
Miscellaneous:1) To see the desktop: press (Window’s Key + M) or (Window’s Key + D)
Navigate around Text:
1) Move to beginning of line: Press Home
2) Move to top: Press CTRL+Home
3) Move to end: Press CTRL+End
Mark (Highlight) the Text:
1) Mark to beginning of line: Press SHIFT+Home
2) Mark to end of line: Press SHIFT+End
3) Mark to top: Press SHIFT+CTRL+Home
4) Mark to end: Press SHIFT+CTRL+End
Window Control:
1) To cascade the windows: Press SHIFT+F5
2) To Tile the windows: Press SHIFT+F4
3) To go to the next window: Press CTRL+F6
4) To close the active window: Press CTRL+F4
5) To exit the SAS system: Press ALT+F4
('’)
Subscribe to:
Posts (Atom)
Learn how to view SAS dataset labels without opening the dataset directly in a SAS session. Easy methods and examples included!
Quick Tip: See SAS Dataset Labels Without Opening the Data Quick Tip: See SAS Dataset Labels With...
-
1) What do you know about CDISC and its standards? CDISC stands for Clinical Data Interchange Standards Consortium and it is developed ke...
-
Comparing Two Approaches to Removing Formats and Informats in SAS Comparing Two Approaches to Removing Formats...
-
USE OF THE “STUDY DAY” VARIABLES The permissible Study Day variables (--DY, --STDY, and --ENDY) describe the relative day of the observ...