Posts

Transporting SAS Files using Proc Copy and or Proc Cport/Proc Cimport

When moving SAS datasets /catalogs from one type of computer to another, there are several things to be considered, such as the operating systems of the two computers, the versions of SAS and the type of communication link between the computers. The easiest way to move SAS datasets from one system to another system is to: Create a transport file using any SAS version. Move the transport file to the new system. Import the transport file on the new system. Transport datasets are 80-byte length binary files made from SAS datasets. PROC COPY or PROC CPORT can create Transport datasets but they both create different types of transport files. Transport files can be created and read using either PROC COPY or PROC CPORT & PROC CIMPORT, but you cannot mix and match. Transport files created with PROC COPY must be read with PROC COPY; those created by PROC CPORT must be read with PROC CIMPORT. PROC COPY uses an engine (i.e. XPORT) to create a SAS transport file. PROC COPY is used to tra...

How to generate the month name from a numeric date value

Image
Task : I have a SAS date and wanted to create a variable with the month name. Here is how to do it...... Use MONNAMEw. format which is simple and easy.  You need to be using  SAS 9.X versions to make it work. /*Use MONNAMEw. format*/ data month; input date: mmddyy8 .; month_name= put (date, monname3 .); datalines; 01/15/04 02/29/04 07/04/04 08/18/04 12/31/04 ; run; proc print; run;

ERROR: The MS Excel table (worksheetname) has been opened for OUTPUT.

I happend to stumbleupon a post from SAS support blog regarding the ERROR message in the LOG file when trying to output a SAS dataset in the form of Excel sheet. Direct link: ERROR: The MS Excel table (worksheetname) has been opened for OUTPUT. This table already exists, or there is a name conflict with an existing object. This table will not be replaced. This engine does not support the REPLACE option. ERROR: Export unsuccessful. See SAS Log for details. When you use the EXPORT procedure on an Excel workbook, the workbook might be corrupted and the following error message generated: This problem can occur if a previous EXPORT procedure attempts to export a SAS data set in the workbook that does not contain any observations. The following example illustrates an export procedure on such a data set: % macro blowup; data a; a=1; stop ; run ; % do i=1 % to 2; proc export data=a outfile =" c:\temp\test.xls " dbms =excel2000 replace ; run ; % end ;...

How to read next record while working on the current record. (LEAD FUNCTION)

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...

STUDY 'DAY' CLCULATION (ONE-LINER)

Image
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. Direct link: 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   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); ...

Easy way to UPCASE variable names of SAS dataset

Image
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")); ...

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 ...