Posts

Delete observations from a SAS data set when all or most of variables has missing data

Image
/* Sample data set */ data missing; input n1 n2 n3 n4 n5 n6 n7 n8 c1 $ c2 $ c3 $ c4 $; datalines ; 1 . 1 . 1 . 1 4 a . c . 1 1 . . 2 . . 5 e . g h 1 . 1 . 3 . . 6 . . k i 1 . . . . . . . . . . . 1 . . . . . . . c . . . . . . . . . . . . . . . ; run; *If you want to delete observation  if the data for every variable is missing then use the following code; *Approach 1: Using the coalesce option inside the datastep; data drop_misobs; set missing; if missing(coalesce(of _numeric_)) and missing(coalesce(of _character_)) then delete ; run ;   Pros: *Simple code Cons; *This code doesn't work if we want to delete observation based on specific variables and not all of them. *Approach 2:Using N/NMISS option inside the datastep; data drop_missing; set missing; *Checks the Non missing values using ; if n(n1, n2, n3, n4, n5, n6, n7, n8, c1, c2, c3, c4)=0 then delete ; run; data drop_missing; set missing; *Checks the missing values us...

Diffrence Between RUN and QUIT statements

Folkes,  Here is the answer from Andrew Karp..... Direct link ****************************************************************************************************; Allow me to weigh in on this topic. It comes up alot when I give SAS training classes. First, RUN and QUIT are both " explicit step boundaries " in the SAS Programming Language. PROC and DATA are "implied step boundaries." Example 1: Two explicit step boundaries. DATA NEW; SET OLD: C = A + B; RUN ; PROC PRINT DATA=NEW; RUN ; In this example, both the data and the proc steps are explicitly "ended" by their respective RUN statements. Example 2: No explicit step boundaries. DATA NEW; SET OLD; C = A + B; PROC PRINT DATA=NEW; In this example, the data step is implicitly terminated by the PROC statement. But, there is no step boundary for the PROC PRINT step/task, so it will not terminate unless/until the SAS supervisor "receives" a step boundary. Some PROCS support what is c...

Sending the LOG and OUTPUT from PC SAS to a seperate file

Image
Here is how to direct the SAS LOG file and or SAS Output  to a seperate file. Approach 1: Using Display Manager Statements; filename log ' C:\temp\logfile.log '; filename out ' C:\temp\output.lst '; *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; DM ' OUT;FILE OUT REP; '; DM ' LOG;FILE LOG REP; '; Information about Display Manager Commands: DEXPORT and DIMPORT: DISPLAY MANAGER commands used to IMPORT and EXPORT the Tab delimited (Excel and .CSV) files; SAS Display Manager Commands Approach 2: Using Proc PRINTTO procedure; Refer:  How to save the log file or what is PROC PRINTTO procedur...

Random Sample Selection

Image
Last week my manager asked me to randomly pick 10%observations from a large data set and then create a listing so that the Data management programmers can QC the data. I want to share some thoughts here … how easy and simple to do random sampling. Approach 1: Data step Approach: In this approach, the observations are shuffled using the RANUNI function which assigns a random number to each observation. Step1 : Generating the Random Vector (shuffling) using the RANUNI function; The RANUNI function generates a random number from a continuous uniform distribution (the interval (0, 1). Step2 : After assigning a random number to each record, the records can then be sorted in ascending or descending order of the random numbers.; data randsamp ; input patno @@; random= RANUNI ( -1 ); * RANUNI function to assign a random number to each record.; * Here the seed is negative integer (-1) so the results are not replicable.; cards; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2...

WARNING: You may have unbalanced quotation marks.

Image
SAS can allow the strings up to 32,767 characters long but some times SAS will write a Warning message ‘ WARNING: The quoted string currently being processed has become more than 262 characters long. You may have unbalanced quotation marks. ’ , when you try to keep a character string longer than 262 characters to a variable.  It is hard to look back at the SAS code to search for unbalanced quotes. To make it more clearly I am going to show an example. I want to add a 263 characters long name to a variable (longvar) and to do that I will simply use a data step… and when I do that I will see the WARNING message in Log. data TEST; x=" (SEE DOCTOR'S LETTER)3RD ADMINISTRATION OF MTX WAS DELAYED BY 14 DAYS AND WAS REDUCED TO 1G/M2 INSTEAD OF 5G/M2, PROBLEMS, E.COLI SEPSIS WITH HEART INSUFFICIENCY WITH SINUS TACHYCARDY, PARALYTIC ILEUS, TACHYPNEA , PATIENT DIED ON 21.04.98 FROM MULTIORGAN FAILURE. "; y=length(x); put x; run ; LOG FILE: There is a SAS option (NOQUO...

CALL EXECUTE: Easy way to print or sort multiple files.

When printing multiple files, or sorting multiple datasets, the traditional method is to write multiple steps as below. Proc print data =libref.ae; var _all_; run; Proc print data =libref.conmed; var _all_; run; Proc print data =libref.demog; var _all_; run; Proc print data =libref.lab; var _all_; run; Proc print data =libref.medhist; var _all_; run; If you are like me who likes to simplify the traditional SAS code here is the tip. CALL EXECUTE comes to rescue here. *Using Disctionary Tables and Call Execute; proc sql ; create table dsn as select distinct memname from dictionary.tables where libname=" LIBREF " and memtype=" DATA "; quit ; *Sorts all the datasets using Call Execute; data _null_ ; set dsn; call execute (" proc sort data=final .||'memname||'; by usubjid; run ;"); run ; *Prints all the datasets using Call Execute; data _null_ ; set dsn; call execute (" proc print data=final .||...

Write a Letter using SAS/ Emailing with SAS

SAS can do many things which most of us don’t have a clue. Here is one example…. Writing a letter: filename formltr ' C:\Documents and Settings\sreddy\Desktop\formltr.rtf ' ; data address; infile datalines; input @ 1 stno  @ 6 lane $12. @ 19 aptno $7. @ 27 city $9. @ 37 state $2. @ 40 zip ; datalines ; 2550 Augusta Blvd Apt#203 Fairfield OH 45014 ; run ; data _null_; retain lm 5; set address; file formltr;* print notitles; put _page_; adr1 = trim(stno) ' ' trim(lane); put @lm adr1; adr2 = trim(aptno); put @lm adr2; adr3 = trim(city) ||', '|| trim(state) ||' '|| trim(zip); put @lm adr3; adr4 = trim('Dear')|| ' ' ||trim('SAS') || ' ' || trim('Users,'); put / @lm adr4; put / @lm ' StudySAS Blog offers a lot of information regarding tips and tutorials on various topics ' ; put @lm ' in SAS. It covers basics to get started to more in-depth topics like Mac...