Showing posts with label Tips and Techniques. Show all posts
Showing posts with label Tips and Techniques. Show all posts
Sunday, 13 March 2016
Saturday, 12 March 2016
Useful Printer System Options
If your printout in the SAS session begins with page 256, it reveals the number of times you ran the program before
printing. Reset the beginning page number before your final run.
To reset the page number, the code is:
To set orientation and paper size:
To remove page breaks and put dashes (-) across the page:
To reinstall page breaks (there is no space between the quotes):
To left justify output:
To reset the page number, the code is:
options pageno=1;
To set orientation and paper size:
options orientation="landscape";
options orientation="portrait";
options papersize="legal";
options formdlim="-";
To reinstall page breaks (there is no space between the quotes):
options formdlim="";
To left justify output:
options nocenter;
Friday, 11 March 2016
Debugging Complex Macros
It’s possible to write code generated by macros to an
external file. The file can’t be access until the SAS session
has ended. In SAS 6.12 you use the options
RESERVEDB1 & MPRINT to achieve this. In version 8
you need to use the options MFILE & MPRINT.
You then
also need to define a fileref for MPRINT, to which the
generated SAS code is written.
filename mprint 'c:\macro.sas' ;
This technique is very useful for complex macros with
many loops and multiple ampersands since you can see
the code that is generated. You can then run the code
generated through data step debugger to resolve any
problems or understand what is happening.
Thursday, 31 December 2015
Specifying the most recently created data set to use in a read operation
By default, the _LAST_= system option is set to use the most recently created data set. This automatic feature is
commonly found in procedure code when the DATA= option is omitted from a PROC.
To override this built-in default, the _LAST_= system option can be defined as any valid temporary or permanent SAS data set name (refer to data set naming conventions).
The _LAST_= option can be specified in an OPTIONS statement, in the OPTIONS window, at SAS invocation, or in the configuration file.
To override this built-in default, the _LAST_= system option can be defined as any valid temporary or permanent SAS data set name (refer to data set naming conventions).
The _LAST_= option can be specified in an OPTIONS statement, in the OPTIONS window, at SAS invocation, or in the configuration file.
Wednesday, 30 December 2015
Writing SAS source files included with a %INCLUDE statement to the SAS Log
By default, SAS source statements included with a %INCLUDE statement are NOT automatically written to the SAS Log. To print secondary SAS source statements included with a %INCLUDE statements, users will need to specify the SOURCE2 system option.
The default setting is NOSOURCE2. Either option can be specified in an OPTIONS
statement, in the OPTIONS window, at SAS invocation, or in the configuration file.
Preventing SAS data sets from accidentally being replaced
Users can specify the NOREPLACE system option to prevent a permanent SAS data set from being accidentally
replaced with another like-named data set. Conversely, by specifying the REPLACE system option like-named data
sets can be overwritten.
The REPLACE | NOREPLACE option can be specified in an OPTIONS statement, in the
OPTIONS window, at SAS invocation, or in the configuration file.
Monday, 2 November 2015
SAS Utility: Drop the Variable if it’s Data is Missing
Drop any variable (Num/Char), if its data is missing in all of the observations…..
%macro DropMissingVars(DS=);
data _null_;
set &ds. (obs=1);
array num_vars[*] _NUMERIC_;
array char_vars[*] _CHARACTER_;
call symputx(‘num_qty’, dim(num_vars));
call symputx(‘char_qty’, dim(char_vars));
run;
data _null_;
set &ds. (obs=1);
array num_vars[*] _NUMERIC_;
array char_vars[*] _CHARACTER_;
call symputx(‘num_qty’, dim(num_vars));
call symputx(‘char_qty’, dim(char_vars));
run;
data _null_;
set &ds. end=finished;
set &ds. end=finished;
array num_vars[*] _NUMERIC_;
array char_vars[*] _CHARACTER_;
array char_vars[*] _CHARACTER_;
array num_miss [&num_qty] $ (&num_qty * ‘missing’);
array char_miss [&char_qty] $ (&char_qty * ‘missing’);
array char_miss [&char_qty] $ (&char_qty * ‘missing’);
length list $ 50;
do i=1 to dim(num_vars);
if num_vars(i) ne . then num_miss(i)=’non-miss’;
end;
do i=1 to dim(char_vars);
if char_vars(i) ne ” then char_miss(i)=’non-miss’;
end;
if num_vars(i) ne . then num_miss(i)=’non-miss’;
end;
do i=1 to dim(char_vars);
if char_vars(i) ne ” then char_miss(i)=’non-miss’;
end;
if finished then do;
do i= 1 to dim(num_vars);
if num_miss(i) = ‘missing’ then list=trim(list)||’ ‘||trim(vname(num_vars(i)));
end;
do i= 1 to dim(char_vars);
if char_miss(i) = ‘missing’ then list=trim(list)||’ ‘||trim(vname(char_vars(i)));
end;
call symput(‘mlist’,list);
end;
run;
do i= 1 to dim(num_vars);
if num_miss(i) = ‘missing’ then list=trim(list)||’ ‘||trim(vname(num_vars(i)));
end;
do i= 1 to dim(char_vars);
if char_miss(i) = ‘missing’ then list=trim(list)||’ ‘||trim(vname(char_vars(i)));
end;
call symput(‘mlist’,list);
end;
run;
data &ds;
set &ds;
drop &mlist;
quit;
set &ds;
drop &mlist;
quit;
%mend DropMissingVars;
%DropMissingVars(DS=work.test);
%DropMissingVars(DS=work.test);
Tuesday, 20 October 2015
Monday, 19 October 2015
Friday, 16 October 2015
Data Step Tip: COMPBL Function
To take extra blanks out of a variable
(character), use the COMPBL function.
Example:
BIGNAME="MARY JANE SMITH"
SMALLER=COMPBL(BIGNAME);
Resulting in:
"MARY JANE SMITH"
Wednesday, 14 October 2015
Data Step Tip: Alignment Option
Analignment specification can be
coded in the PUT statement.
Code-L,-C,or-R after the format.
Example:
124 DATA _NULL_;
125 SET NAMES;
126 PUT @1 NAME $30. -C;
127 RUN;
KATIE
TERESA
STEVE
ANN
NOTE: There were 4
observations read from the data set WORK.NAMES.
Tuesday, 13 October 2015
Data Step Tip: Trailing @ For Efficiency
Use the trailing "@" with input statements to avoid
inputting records you wish to eliminate.
Instead of:
DATA MIDWEST;
INFILE ACCOUNTS;
INPUT @1 NAME $19.
@20 STATE $2.
@24 ACCTNBR $10.
@35 BALANCE 8.;
IF STATE IN ('WI','MN','IA');
RUN;
Better:
DATA MIDWEST;
INFILE ACCOUNTS;
INPUT @20 STATE $2. @;
IF STATE IN ('WI','MN','IA') THEN
INPUT @1 NAME $19.
@24 ACCTNBR $10.
@35 BALANCE 8.;
RUN;
• More Efficient
• Different Layouts in One File
Data Step Tip: Input @ ‘TEXT’
Using INPUT @ ' text' positions the input pointer directly
after the word 'text'.
DATA NAMES;
LENGTH NAME CITY $10 ;
INPUT @1 NAME @ 'CITY=' CITY AGE ;
DATALINES;
ABC CITY=BGLR 25
DEFGH CITY=HYD 30
IJKLMNO CITY=BGLR 28
PQRS CITY=HYD 26
;
RUN;
PROC PRINT DATA=NAMES;
RUN;;
output
Obs NAME CITY AGE
1 ABC BGLR 25
2 DEFGH HYD 30
3 IJKLMNO
BGLR 28
4 PQRS HYD 26
If the text is not found, the pointer will go to a new line
DATA NAMES;
LENGTH NAME CITY $10 ;
INPUT @1 NAME @ 'CITY=' CITY AGE ;
DATALINES;
ABC CITY=BGLR 25
DEFGH CITY=HYD 30
IJKLMNO BGLR 28
PQRS CITY=HYD 26
;
;
RUN;
PROC PRINT DATA=NAMES;
RUN;
output
Obs NAME CITY AGE
1 ABC BGLR 25
2 DEFGH HYD 30
3 PQRS HYD 26
in above example IJKLMNO is missing because the string CITY=
not found,so the pointer will go to a new line.
PC SAS SHORTCUTS
When you are working on PC SAS there are many helpful keyboard shortcuts available at your disposal. For example when you need to comment or un-comment blocks of code, you can save time by using the shortcut instead of typing it all yourself. Or when you are trying to find the matching pair in a set of brackets, simply using the shortcut means that you will not need to spend the time searching yourself.
These actions can be performed with ease by pressing a few buttons on the keyboard. The following table shows some of the shortcuts available to use:
Action
|
Keyboard Shortcut
|
Convert highlighted text to upper case
|
Ctrl Shift U
|
Convert highlighted text to lower case
|
Ctrl Shift L
|
Comment highlighted text
|
Ctrl /
|
Uncomment highlighted text
|
Ctrl Shift /
|
Collapse all sections of code
|
Ctrl Alt – (on number pad)
|
Expand all sections of code
|
Ctrl Alt + (on number pad)
|
Move cursor to matching bracket
|
Ctrl (
|
Move cursor to matching DO or END statement
|
Alt [
|
The full list of available shortcuts can be accessed through the menu:
Tools -> Options -> Enhanced Editor Keys
This will show a list of all commands that have a keyboard shortcut assigned to them, along with a brief description of what the command will do.
Tools -> Options -> Enhanced Editor Keys
This will show a list of all commands that have a keyboard shortcut assigned to them, along with a brief description of what the command will do.
- Convert highlighted text to opposite case
- Insert current date and time
- Delete line
- Repeat the current line
- Remove trailing white space from end of lines
The Assign keys… button can be used to set a shortcut or to change an existing shortcut.
Personally I find switching text to upper/lower case and commenting/un-commenting code to be especially useful and a good time saver.
CONCLUSION
These tips are all easy to learn, quick to use and great to share with other programmers. I would recommend trying these out to see if they can help you in your day to daySAS Programming
For More Click here
Monday, 12 October 2015
HOW TO ACCESS SPECIAL CHARACTERS IN SAS
There may be times when you need a specific character used in your outputs, for example superscripts or trade mark symbols in a footnote, but cannot see these directly on your keyboard. These characters and many more are all accessible for use in your SAS programs.
This table shows some of the special characters available to use, along with their corresponding byte number in SAS and a description.
This table shows some of the special characters available to use, along with their corresponding byte number in SAS and a description.
Byte(i)
|
Symbol
|
Description
|
153
|
™
|
Trademark sign
|
169
|
©
|
Copyright sign
|
170
|
ยช
|
Superscript a
|
174
|
®
|
Registered trademark sign
|
176
|
°
|
Degree symbol
|
177
|
±
|
Plus or minus sign
|
178
|
²
|
Superscript 2 / squared
|
179
|
³
|
Superscript 3 / cubed
|
185
|
¹
|
Superscript 1
|
188
|
¼
|
One quarter
|
189
|
½
|
Half
|
190
|
¾
|
Three quarters
|
247
|
÷
|
Division sign
|
The full list of characters can be seen by running the following code. This will create a dataset with a variable containing each special character and a variable with each respective byte number in SAS.
data check;
do i=1 to 255;
sq=byte(i);
output;
end;
run;
Once you know which character you want in your program, then you can use %let to create a macro variable of it. For example the following code will create a macro variable for the superscript a symbol:
%let supa=%sysfunc(byte(170));
If you want to check this has worked properly, then use %put to write the value of this macro variable to the log:
%put &supa;
The macro variable can then be inserted into your program. For example the following is standard proc report code for creating a table, but using this macro variable to create a superscript value in the footnote. The highlighted code shows where the macro variable is used.
proc report data=final nowd headline headskip split=' | ' ps=23formchar(2)='_';
column ('__' pdcdl col1 ord1 ord2);
define pdcdl / left ' ' width=60;
define col1 / center 'DrugA|6 mg SC';
define ord1 / order noprint;
define ord2 / order noprint;
title 'Table 14-1.3 Summary of Important Protocol Deviations';
compute after _page_;
line @13 72*"_";
line @2 ' ';
line @14 "&supa Other than Treatment Compliance/Test Article Administration";
line @14 'Note: Deviation categories are not mutually exclusive';
endcomp;
break after ord1 / skip;
run;
This will create the following output. The highlighted part shows what the footnote looks like from inserting the special character into it.
As you can see in the example, it is a simple process to find out the byte number of the desired special character and then use it in your program.
Subscribe to:
Posts (Atom)


