Posts

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)

HOW TO CREATE A TRANSPORT FILE

Image
CREATING A TRANSPORT FILE:

In SAS, how do I create a transport data set file?

In SAS, how do I create a transport data set file? Source/direct link: http://kb.iu.edu/data/aevb.html A SAS transport data set file is a machine-independent file that allows you to move a SAS data set from one operating system to another. A SAS transport data set file can also be read directly by several statistical software packages (e.g., SPSS, BMDP). Following is an example of SAS code to copy the SAS data set file job1.sas7bdat to a SAS transport data set file portable.xpt in the outdata directory: LIBNAME misc ' ~/work' ; LIBNAME sasxpt XPORT ' ~/outdata/portable.xpt' ; PROC COPY IN=misc OUT=sasxpt ; SELECT job1; RUN ; In the example above: The first LIBNAME statement aliases the library reference (libref) misc to the work directory. The second LIBNAM E statement aliases the libref sasxpt with the physical name of a SAS transport format file (in this case, portable.xpt in the outdata directory). The COPY procedure copies one or more SAS data sets in the IN = l...

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

How to detect missing values using ARRAYS

Using an array in SAS to detect missing values Direct link: http://ssc.utexas.edu/consulting/answers/sas/sas65.html Question: How do I exclude observations from my PROC FREQ analysis when a value is missing from a list of variables? Answer: In the SAS DATA step, you can create a new variable ("miss" in the example below) that is set equal to 1 when a variable has a missing value, 0 otherwise. Use the ARRAY statement and a DO loop to check for missing values across a list of variables; the syntax is: DATA one ; INFILE xxx; INPUT a b c d e; miss= 0 ; ARRAY vv(5) a b c d e ; DO i= 1 TO 5 ; IF vv(i)=. THEN DO ; miss=1 ; i=5; END; END; RUN; PROC FREQ; WHERE miss =0; TABLES a b c d e ; RUN ; Here, the array "vv" has 5 elements (a,b,c,d,e), and the loop "i" is likewise set to 5. For each observation, the loop iterates 5 times, checking for missing values across the list of 5 variables. When a missing value ...

SAS System Options in the UNIX environment

SAS System Options in the UNIX environment Direct link: http://ssc.utexas.edu/consulting/answers/sas/sas55.html Question: How do I specify SAS system options in the UNIX environment? Answer: How you specify SAS system options depends on how you use SAS in the UNIX environment. If you use SAS via an X-terminal or X-terminal emulation software such as Exodus or MacX, the command to launch SAS on ITS UNIX systems is /usr/local/sas/sas SAS system options are preceded by a hyphen and immediately follow the SAS command. For example, if you want to have SAS write its work files (including temporary datasets) to a directory called "mysasdir" located one level below your own current working directory, the syntax for invoking this option would be: /usr/local/sas/sas -work ./mysasdir If you use the SAS display manager system via a vt100 terminal interface such as telnet, the usual command to launch SAS is: /usr/local/sas/sas -fsd ascii.vt100 The -fsd portion of this command is a SAS opt...