Posts

Learn SAS Programming

How to determine the executing program name and path programatically: Oftentimes, I was asked to keep the name and path of the executing program in the FOOTNOTE of the generated table or listings.I have always created a macro variable using the %let statement and, then I called the Macro variable in the footnote statement to get the name of the program. Eventhough it is simple.. it may not be suitable when we write application which need to self document... Read more at: http://studysas.blogspot.com/2009/04/how-to-determine-executing-program-name.html ________________________________________________________________________________ How to check if a variable exist or not: In SAS sometimes, we need to check whether the variable is exist in the dataset or not, we usually run the proc contents program and physically check if the variable exist in the dataset or not.If we want to check it programmatically, then use the following code.... Read more at: http://studysas.blogspot.com/2009/04/ho...

How to determine the executing program name and path programatically

Sometimes, we need to keep the name and path of the executing program in the FOOTNOTE of the generated table or listings. I have always created a macro variable using the %let statement and, then I called the Macro variable in the footnote statement to get the name of the program. Eventhough it is simple.. it may not be suitable when we write application which need to self document... Here is another technique which can be used to retrieve the pathname and filename (last executed) ..... To get the last opened filename: proc sql noprint; select scan (xpath,-1, ' \ ') into :progname from sashelp.vextfl where upcase( xpath ) like '%.SAS' ; quit ; %put &progname; result: filename.sas or data _null_; set sashelp.vextfl( where = ( upcase ( xpath ) like '%.SAS' )); call symput ( 'program' , scan ( xpath , - 1 , '\' )); run ; %put &program; result: filename.sas To get path name ... then .. use the following code; ...

How to Check, if the variable exists in the SAS dataset or not

How to check if a variable exist or not: In SAS sometimes, we need to check whether the variable is exist in the dataset or not, we usually run the proc contents program and physically check if the variable exist in the dataset or not. If we want to check it programmatically, then use the following code.... Sample dataset: data old; input ID SCORE1 SCORE2 SCORE3 SCORE4 SCORE5 SCORE6; cards; 24 100 97 99 100 85 85 28 98 87 98 100 44 90 60 100 97 69 100 100 100 65 100 98 100 93 90 100 70 99 97 100 100 95 100 40 97 99 98 49 100 95 190 100 99 97 100 100 90 196 100 100 99 100 100 100 210 98 85 88 90 80 95 100 ; run ; data _null_; dset= open ( ' old ' ); call symput (' chk ',varnum ( dset ,' SCORE4 ')); run ; %put &chk; RESULT:5 Here I have used both OPEN and VARNUM functions in the program to check if SCORE4 variable exists in a 'OLD' dataset. The OPEN function opens a SAS data set and the VARNUM function, which returns the variable's position ...

How to check if the File is exist or not in SAS

How to check if the File is exist or not in SAS: Guys… Let me try explaining how to check if the file exist in the library or directory using SAS. Here I am writing a macro to check if the file exist in the directory or not. Here is the way to check it… % macro check(dir= ) ; %if %sysfunc ( fileexist (&dir)) %then %do; % put File Exists ; % end ; %else %do ; %put File doesn’t exist.; % end ; % mend ; %check (dir=" C:\Documents and Settings\sannapareddy\Desktop\Holidays 2009.docx" ); %sysfunc option empowers me to use the same Data step functions outside the Data step. Almost all of the Data step functions are available to use outside the data step if we use %sysfunc. fileexist option, verifies the existence of an external file. It can also verifies the SAS library. Here is another simple technique to check..... data _null_ ; x= filexist( 'C:\Documents and Settings\sannapareddy\Desktop\Holidays 2009.docx' ); call...

Importance of Warnings and Notes messages from SAS log

The errors I will list here will be very few in number. They are errors that you will likely make at some time if you do not remain alert. These errors could have serious consequences so that is why I have described them as "deadly errors". Missing "by" statement in merge: Using "set" instead of "merge": Existing "in" variable in merge: "Flag" variables in data steps: "Fixing" data during a merge: Averaging averages: Merging in Key Variables: Read more at........... Importance of Warnings and Notes messages from SAS log A Utility Program for Checking SAS Log Files

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