Posts

Proc Sort NODUP vs NODUPKEY

Image
The SORT Procedure (Proc Sort): Options We can do many things using PROC SORT like create a new data set ,  subset the data ,  rename/ drop/ keep variables ,  format, or label your variables  etc Options Available with Proc Sort: OUT= OPTION DESCENDING OPTION DROP=, KEEP=, AND RENAME= OPTIONS  FORMAT AND LABEL STATEMENTS WHERE= OPTION OR WHERE STATEMENT FIRSTOBS= AND OBS= OPTIONS NODUPRECS AND NODUPKEY OPTIONS DUPOUT A common interview question for SAS jobs is "What is the difference between proc sort nodup and proc sort nodupkey?". The answer the interviewer is expecting is usually "proc sort nodup gets rid of duplicate records with the same sort key but proc sort nodupkey gets rid of other records with the same sort key". However, this is not correct. Read mo re at……. Common Programming Mistake with Proc Sort NODUPRECS - Equivalent of NODUPKEY in PROC SQL Ian Whitlock Explains... NODUPKEY is like FIRST. processing. Both d...

PROC TRANSPOSE: How to Convert Variables(columns) into Observations(ROWS) and Observations(ROWS) into Variables(Columns)

Image
During my early days as a SAS programmer, I used to get confused which statement in PROC TRANSPOSE used for what and why? PROC TRANSPOSE syntax looks like a straightforward thing, but we need to look at important details it offers and without that we may get unexpected results. Proc Transpose Options: Proc Tranpose offers several options like OUT=, LABEL=, NAME=, and PREFIX=. Each option is distinct from the others. "OUT=" option assigns an output dataset, "LABEL=" option assigns a nemae to the variable which has the label of the transposed variable. If we don’t use "LABEL=" option in the PROC TRANSPOSE syntax, the defalut “_LABEL_” will be assigned to the variable that is being transposed. "NAME= " option works same as the "LABEL=" option, as if we use NAME=option in the TRANSPOSE syntax, which will assign the name to the variable that is being tranposed. There is another option that we can use in the TRANSPOSE syntax that ...

Displaying the Graphs (Bar charts) using PROC GCHART in SAS

Image
Displaying the Graphs (Bar charts) using PROC GCHART in SAS : Just a day ago, I've received a question on Graphs from one of the member of  my orkut community , which prompted me to create following example. Below are 5 different types of Graphs were produced (including 3d graphs) using Proc Gchart. Sample data set used for the examples: data x; input quarter $ ncl ram; cards; q1 20000 2000 q1 30000 3000 q1 45000 2000 q2 23000 2003 q2 45000 4500 q3 46000 5600 q3 89000 600 q3 67000 4500 q4 45890 890 q4 46000 9800 ; run ; *Example1: Simple barchart; /* Add a title */ title 'Sales Report'; /* Produce the bar chart */ proc gchart data=x; vbar quarter / sumvar=ncl nozero ; format ncl dollar8.; run ; quit ; Snapshot of the output: *Example2: Producing 3D graphs with cylinder shape using Proc Gchart; pattern c=blue; axis1 minor=none label=('NCL Sum'); title 'Bar Chart Shape'; /* use the SHAPE= option to specify a cyl...

Change all missing values of all variables into zeros/putting zeros in place of missing values for variables

Have you been asked how to convert missing values for all the variables into zeros..... if you are.... here is the answer for that..... In this example the I have used array to do the same. The variable list includes ID and Score1 to score6.Using simple array method we can change all the missing value for the variables score1 to score6 to 0. data old; input ID SCORE1 SCORE2 SCORE3 SCORE4 SCORE5 SCORE6; cards; 24 100 97 . 100 85 85 28 . 87 98 100 . 90 60 100 . . 100 100 100 65 100 98 100 . 90 100 70 99 97 100 100 95 100 40 97 99 98 . 100 95 190 100 . 97 100 100 90 196 100 100 . 100 100 100 210 . 85 . 90 80 95 ;   run ; *Ist Method; data new; set old; array zero score1-score6; do over zero; if zero=. then zero=0 ; end ; run ; *2nd Method; data new; set old; array nums _numeric_; do over nums ; if nums=. then nums=0 ; end; run ; proc print; Title 'Missing values changed to zero using arrays and a do loop' ; run ; Output: I...

Sending the SAS Log to an Output File

Here is the simple code which allows us to save the log file in the required location. Use Proc printto procedure to save or print the log file. filename dsn ‘ C:\Documents and Settings\zzzzzzzzzzzz\Desktop\LOGfile.lst' ; data dsn ; input patid implants ; datalines; 1 3 1 1 1 2 1 1 2 1 2 2 3 1 4 2 3 1 4 5 2 3 1 6 ; run ; proc printto log= dsn new ; run ; Example 2: proc printto log =" C:\temp\LOGLOG.log " print =" C:\temp\LSTLST.lst " new ; run; *Select only male students and age less than 16; proc sql; create table males as select age, height, weight from sashelp.class where sex=' M ' and age lt 16 order by age; quit; *Get the descriptive statistics for height variable by age; proc means data =males ; by age; var height; output out =htstats mean =mean n =n std =sd median =med min =min max =max; run; PROC PRINTTO; * The second PROC PRINTTO step redirects output back to its default destination ....

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 ;