Posts

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

PROC SQL basics, tips and techniques and sample code programs

Proc SQL: Power of SAS SQL: • SQL looks at datasets differently from SAS – SAS looks at a dataset one record at a time, using an implied loop that moves from the first record to the last – SQL looks at all the records, as a single object • Because of this difference SQL can easily do few things that are more difficult to do in SAS • There are a number of built in functions in SQL that can be used in a select statement • Because of how SQL handles a dataset, these functions work over the entire dataset • Functions: – Count: Counts Values – Sum: Sums Values – Max: Identifies the largest value – Min: Identifies the smallest value – Mean: Averages the values Read more at www.cognigencorp.com/perspective/tipsNtricks.pub/1/PROC%20SQL%20Talk_12_.ppt – SAS SQL Introduction to Proc SQL AN INTRODUCTION TO PROC SQL® PROC SQL: When and How to Use It? Proc SQL – A Primer for SAS Programmers Understanding PROC SQL Creating Macro Variables with Proc SQL DATA Step vs. PROC SQL: What’s a neophyte to d...

Length of Numeric variables GT 8 in SAS| StudySAS BLOG

Q&A: numeric variables length more than 8? We all know that the default length of the numeric variables in SAS is 8 and if suppose I want to store a number lets say (12345678910, which has a length 11 to numeric variable) to variable total, what should I do? What if the numeric variable digits are more than 12 digits and i want to store them all without any E values? ANS) The default length of numeric variables in SAS is 8 and all the numbers that we see in the sas datasets are called as floating numbers(floating point binary) and not a regular sequence numbers form 1 to 10. When we are using SAS/Windows as our operating system and then the minimum length for any numeric variable should be 3(not 1 as we get confused all the time). So if a variable contas less than 3 digits means it is stored with less space. The reason is, since a numeric variable will need a power and and the sign(+ or -), if SAS want to store a numberit defenitely needs a minimum of 3 bytes. Depending ...

Options VALIDVARNAME=UPCASE

VALIDVARNAME= V7 UPCASE ANY VALIDVARNAME= option is generally used in SAS whenever we want to control the SAS variable names in the dataset. VALIDVARNAME= V7 UPCASE ANY The default option will be VALIDVARNAME=V7 until we specify as UPPERCASE or ANY . When we mention options VALIDVARNAME=V7 , that means we are telling SAS to change the name of the Database column (etc EXCEL sheet column) to valid SAS name with certain rules keeping in mind. Here are those rules that SAS needs to follow, when it changes the DBMS column name to valid SAS name. Only 32 mixed case (lower or uppercase) characters are allowed in each variable. Names should be starting with an underscore or an alphabet (either uppercase or lower case character). Invalid characters in the DBMS column (ex. $) should be changed to underscores. See the SAS Language Reference: Dictionary to get more details about the rules. VALIDVARNAME=UPCASE When we mention options VALIDVARNAME=UPPERCASE we are telling SAS to change the column...

How to merge data sets with a common variable?

Here is the simple way of merging the data sets with a common variable if the datasets has the same prefix name. For example: col1-col10, dsn1-dsn 7 , or data1 to data10 with common variable of ID. Considering we have 10 datsets and all of them having the same prefix data; %macro mymerge (n); data merged; merge %do i = 1 % to &n; data&i %end; ; /* this additional ';' is necessary, the first ';' is for the "%end", while the second ';' is for "Merge"*/; by id; run; %mend; %mymerge(10)

Merging the data sets using macro code

Merging the data sets with a common variable if the datasets has the same prefix name? For example: col1-col10 dsn1-dsn 7 data1 to data6 with common variable of Usubjid. here is the example, I have 7 datasets i need to merge and each of them having the common variable(usubjid) to merge, and all the datasets having the same prefix dsn(dsn1 to dsn7). % macro allmerge (n); data combine ; merge %do i = 1 % to &n; dsn&i %end; ; /* this additional ';' is necessary, the first ';' is for the "%end", while the second ';' is for "Merge" */ by usubjid; run ; %mend; %allmerge (7)