February 21, 2013
Set SAS to display variable names not labels
I don't understand this default setting but it can be changed.
- Click in or on the Explorer pane to highlight the the Explorer window.
- Select Tools->Options->Explorer in the menus.
- Select the Members tab.
- Double click on the TABLE icon.
- Double click on the &Open action.
- Set the Action command to: VIEWTABLE %8b.’%s’.DATA COLHEADING=NAMES
- Click on the Set Default button.
- Save changes and close the Explorer Options window
January 29, 2013
Debugging in SAS
I'm working through some tutorials and this seems to be an important topic.
putlog _all_; *prints the current pdv (working data row) to the log;
the _ERROR_ variable increments to one if a syntax error occurs. You can use conditional logic to output variable information to the log.
putlog 'WARNING: blah blah'; * will colorcode the output
data customers /debug; *will enter debug mode;
- doesn't work with SAS Enterprise Guide
Step - goes to nex step
Watch or W [variable] - auto-print variable value when it changes
Deletewatch or DW [variable]
Examine or E [variable] - print a variable value
$ examine varible $quote7. - print variable within quotes and up to 7 characters
List or L [_All_ Files Break INFILES Datasets WATCH] - print all items within the specified option
Set [variable] = [value] - change a variable value
List or L
set [dataset] END=last; * when the data step cycles through the last observation of the dataset 'last' will be set to one. Conditionals can be added to do something special upon reaching the last row;
January 28, 2013
total = sum(of Qtr1-Qtr4); *will hit Qtr2, Qtr3, etc.;
specify columns in between those specified
total = sum(of Qtr1--fourth);
total = sum(of Qtr1-numeric-fourth); *only 'numeric' (can use 'character' too) between the variables specified;
all columns beginning with specified string
total = sum(of Tot:);
Total = sum(of _All_); *Can also use _Numeric_ or _Character_;
SAS FAQ #31346987234: The DSD Infile option
Using the DSD option as part of an infile statement will cause sas to ignore "delimiters" that may be found within a quoted text string.
Two consecutive delimiters will lead to a missing value with DSD.
January 25, 2013
Read variables or datasets with names containing spaces
SAS hates spaces - which I am fine with - but sometimes a variable or dataset exists with spaces in it's name. To get around this put the name/variable into quotes followed by an 'n'.
proc contents data=work.'poorly named dataset'n;
January 18, 2013
Create random number string in SAS
Adapted code from here
%let NObs = 52;
call streaminit(123); /* random num generator seed*/
Min = 100000; Max = 999999; /*range of random number*/
do i = 1 to &NObs;
psid = min + floor((1+Max-Min)*rand("Uniform")); /* uniform integer in Min..Max */
January 07, 2013
Importing files in SAS
This will import a comma delimited file. Note that input is required and can be used to specify only certain variables that should be part of the dataset.
infile 'J:\blaise_sample_files\mixed_mode\5_sample_set\sample_other.csv' dlm=',';
input var1 var2;
This SAS pdf has more.
There is also a PROC IMPORT statement but I'm not sure when you would use one approach over the other.
PROC IMPORT OUT=work.test FILE = "J:\blaise_sample_files\mixed_mode\5_sample_set\sample_other.csv" DBMS = CSV REPLACE; GETNAMES = YES; DATAROW = 2; RUN;
Where statements in the DATA step
Select a subset of data set that contains only NULLS:
where PushStringToMainIw = ' ';
drop DOB TIMEZONE;
Select subset of data where a given column contains a value. This gets into some regex for SAS
- the percent sign (%) replaces any number of characters
- the underscore (_) replaces one character
where PushStringToMainIw LIKE '10-1xxx-003%' ;
January 04, 2013
_n_ variable in SAS
Creating a post on this because the interwebs appear to be a bit crossed.
One source indicates
The automatic variable _n_ represents the observation numbers.
While another indicates
Each time the DATA step loops past the DATA statement, the variable _N_ increments by 1. The value of _N_ represents the number of times the DATA step has iterated.
I suppose these are equivalent statements if the data step iterates for every observation in the data file.
Useful shortcut commands in SAS 9.x
In lieu of hotkeys to toggle between SAS windows I am going to try using the Command prompt (with command shortcut set to ctrl+j).
A somewhat complete list of commands from the SAS people.
Commands to open a new Enhanced Editor or to toggle to the current Editor.
WPGM to go back to original enhanced editor window
WEDIT to create a new enhanced editor window