Posts

SAS Date,Time and datetime functions

A Beginners Guide to SAS Date and Time Handling: Abstract: The SAS system provides a plethora of methods to handle date and time values. Correct date computation became a very important subject when the world became aware of the Year 2000 issue. Computer users now realize most applications contain date and time variables. This beginning tutorial describes how the SAS system 1) stores dates, datetimes, and times; 2) reads date/time variables from "Raw Data Files" and from SAS data sets. It covers when and where to use SAS Informats and formats. Next it describes the easy methods to perform date/time arithmetic via date/time SAS functions. The paper shows how SAS date Formats can be used in SAS Procedures such as PRINT, FREQ, and CHART. Finally the tutorial will discuss Year 2000 issues and how SAS software helps you maintain correct date integrity and prevent future Y2k problems in the new millennium. More can be read at: http://www.kellogg.northwestern.edu/researchc...

Calculating group totals and the counts within each group

Image
Sample 25217: Calculating group totals and the counts within each group This example uses the SUM() function to sum the AMOUNT column, creating a new column named GRPTOTAL with a COMMA10. format. The COUNT() function counts the number of occurrences of STATE within each group. The GROUP BY clause collapses multiple rows for each group into one row per group, containing STATE, GRPTOTAL and the COUNT. data one ; input state $ amount; cards; CA 7000 CA 6500 CA 5800 NC 4800 NC 3640 SC 3520 VA 4490 VA 8700 VA 2850 VA 1111  ;    proc sql; create table two as select state ,sum( amount ) as grptotal format = comma10. , count(*) as count from one group by state ; quit ;  proc print data=two noobs; run ;  

How to customize page numbers in RTF output

Usage Note 24439: In SAS 9.1, are there easier ways to customize page numbers in RTF output? direct link here http://support.sas.com/kb/24/439.html Yes, beginning with SAS 9.1, page numbers can be customized in the RTF destination by using an escape character and the {thispage} function, {lastpage} function, {pageof} function, or all three: ods escapechar=' ^ '; ods listing close; ods rtf file=' c:\tests\test.rtf '; data test ; do i= 1 to 50 ; output; end; run ; proc print data=test noobs; title ' Page ^{thispage} of ^{lastpage} '; footnote ' ^{pageof} '; run ; ods listing; ods rtf close;

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

How to calculate number of years and number of days between 2 dates; Exploring the yrdif and datdif functions in SAS as well as INTCK function: There are several ways to calculate the number of years between two dates and out of all the methods, YRDIF function results the most accurate value. Syntax: ageinyrs = YRDIF(birthdate, enddate, ' act/act '); ag_indays = DATDIF(birthdate, enddate, ' act/act '); “ act/act ” will gives us the actual interval between the two dates. The YRDIF function returns the calculated years between two SAS date values. The returned value will be a precise calculation, including multiple decimal places. Whereas with INTCK function will just give the rounded value like 10, 11 and not like 10.2 and 11.5. Syntax: Using YRDIF function: To know the interval between two dates in Years: data _null_; sdate=" 12mar1998 "d; edate=" 12jun2008 "d; years=yrdif(sdate,edate, 'act/act' ); put years ; run ; ...

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

IN SAS programming, we often require outputting the dataset in different formats like EXCEL and CSV etc and here are the five different ways to export the SAS dataset into .csv file. Example: data new ; infile datalines dsd dlm=' ' missover; input a b c d; datalines; 3 5 1 1 4 1 . . 5 8 3 2 6 0 4 4 ; run ; By putting MISSOVER in the infile statement we are telling SAS to do not look for the data in the next lane if it runs out of the data, instead keep missing values for any remaining variables. DSD and DLM options should be included always in the infile statement, if we include the dlm=’ ‘ in the infile statement then SAS will put one digit for each variable even though we haven’t assigned any length to variable. DSD option will tell SAS to consider a missing value if 2 delimiters are present side by side in any observation. When we ran the above program in SAS, we create a SAS dataset name ‘ NEW’ in the work directory and if we want to create a ...

How to Import Excel files into SAS

Reading from Excel Spreadsheets: Microsoft Excel spreadsheets can be read from SAS in several ways. Two of these will be demonstrated here. First, PROC IMPORT allows direct access to Excel files through SAS/Access to PC File Formats or access to Comma-Separated (CSV) files through Base SAS. The second method uses the Excel LIBNAME engine. PROC IMPORT The IMPORT procedure reads from external sources and creates a SAS data set. Two sources are Excel spreadsheets and CSV files. A particular SAS/Access product may be required for certain sources, however. In our example, SAS/Access to PC File Formats is required to read an Excel file, but a CSV file can be accessed with Base SAS. General Syntax for PROC IMPORT: PROC IMPORT DATAFILE=" c:\sas\ego.csv " OUT=jeeshim.egov DBMS=CSV REPLACE; For Excel you use the DATAFILE =”filename” option to specify the Excel file to be read. (The TABLE=”tablename” option would be applicable if you were reading from a database such as Microso...

THE SPECIAL “??” FORMAT MODIFIER

The following excerpt is from SAS OnlineDoc documentation: ? or ?? Direct link: http://www.nesug.org/Proceedings/nesug01/at/at1013.pdf The optional question mark (?) and double question mark (??) format modifiers suppress the printing of both the error messages and the input lines when invalid data values are read. The ? modifier suppresses the invalid data message. The ?? modifier also suppresses the invalid data message and, in addition, prevents the automatic variable _ERROR_ from being set to 1 when invalid data are read. Below is an example of using ?? to determine whether a variable contains non-numeric values or not: data _null_; x = “ 12345678 ”; if (input (x, ?? 8. ) eq . ) then put ‘ non-numeric’ ; else put ‘numeric’ ; run ; Running SAS would return “Numeric” in the above example. If we used X=”123a5678”, SAS would return “Non-Numeric”. Note that the input format in the above example is “8.” So only the first 8 bytes of the character string are checked...