Exporting without double quotes
One of our junior programmers shot this one over after fighting with Proc Export for some time to remove the quotes from a string.
His need: export a single column of data without headers or quotes.
How he did it, quick and easy.
data _null_;
set inputdateset;
file "outputfilename";
put outputcolumnname;
run;
Sometimes the simple solutions are right in front of us.
Default value to macro variable
NOTE: This is a great one we picked up from our friends over at the SAS community.
We have run across this literally hundreds of times while programming SAS macros. You need to have a default value for a variable and you don't want to write another macro to set it if it doesn't exist.
This simple fix allows you to check for a value and set a default even in open SAS code.
%global myParameter; /* ensure it exists */ %let myParameter = %sysfunc(coalescec(&myParameter,default-value));
Pure awesomeness..
Credit goes to Don Henderson and the SAS community for this one.
http://www.sascommunity.org/wiki/Tip_of_the_Day:October_12
SAS – Lowercase (lowcase) / Uppercase (upcase) / Proper Case (propcase)
We can't stress the importance handling character case correctly. Here are the two functions you need to know and use correctly.
In order to convert all characters in a sting to lowercase, use the LOWCASE function. Example:
data ds_2;
set ds_1;
*convert it to lowercase;
new_char_var=lowcase(OLD_CHAR_VAR);
run;
In order to convert all characters in a sting to uppercase, use the UPCASE function. Example:
data ds_2;
set ds_1;
*convert it to uppercase;
NEW_CHAR_VAR=upcase(old_char_var);
run;
We can't stress the importance of keeping this functionality in mind. Many a SAS script has gone awry because a developer didn't take character case into account when using conditional or search statements.
TIP: A solid programming practice is to convert everything to lowercase before storing it. There are exceptions to this (like names, etc), but they can be handled on a case basis.
Also note the PROPCASE function. It capitalizes the first letter of each word in a string. Example:
data ds_2;
set ds_1;
New_Prop_Var=propcase(old_char_var);
put New_Prop_Var;
run;
This Is The New Prop Var.
How to remove characters with SAS COMPRESS
Compress is a string function in SAS. This function doesnt make your characters smaller but it does take some of them out. Here is the basic example.
data _null_;
string=Here is a string;
new_str=compress(string,'e'); /*now new_str= Hr is a string */
run;
Pretty simple huh? But wait, in SAS v9 there are new features. A third function argument even. Example? Sure:
data _null_;
string=Here is a string;
new_str= compress(string,'eia',"k"); /* now new_str=eeiai */
run;
This third argument breaks down like so:
a - all upper and lowercase letters
d - all numeric digits
i - ignores case
k - keeps the listed characters instead of removing them
s - adds all blank space to the list (like tabs, spaces, carriage returns)
p - adds all punctuation to the list
u - adds all uppercase letters
l - adds all lowercase letters
Suddenly this function is more useful than ever.
If you have some SAS code you would like help on, we are always available for short and long term consulting jobs. No project is too large or small.
The SAS Scan Function
Here is a useful function for you. The SCAN function. Scan allows you to isolate a word ( or group of letters ) in a text string given a definable set of delimiters. Here is an example.
data _null_;
string='Here is my string';
first_word=scan( string, 1, ' ' ); **First word in a string;
last_word=scan( string, -1, ' ' ); **Last word in a string;
run;
Keep in mind that a word is simply defined as a group of letters between two delimiters. So you can scan strings using various tricks.
data _null_;
string='Hello SAS community people';
beginning= scan( string, 1, 'S' ); ** returns "Hello ";
middle = scan( string, 2, 'S' ); ** returns "A";
end= scan( string, 3, 'S' ); **returns " community people";
run;
AFHood Analytics Group has consultants on staff to help with your SAS scripts. We can work with you on a small fix or a large development projects. Contact us to discuss how we can work together.
Search a String Variable with the SAS Index Function – INDEX, INDEXC, INDEXW
The SAS index function is a basic tool for any programmer. The job of the index function is to search a string variable for a given value. The function returns a numeric value for the position of the value that matches your search criteria. In the event that there are multiple matches, it will return the first.
Here is a basic example:
proc print data = an_old_dataset; run; Obs var_name 1 AFHood 2 Analytics 3 Group 4 AAG 5 AFHood Analytics Group
data a_new_dataset;
set an_old_dataset;
got_value = index(var_name,"AFHood");
run;
Obs var_name got_value 1 AFHood 1 2 Analytics 0 3 Group 0 4 AAG 0 5 AFHood Analytics Group 1
There is another function that is similar and equally as useful. That is indexc. The function will return the first occurrence of any of the characters in any of the search strings.
data a_new_dataset;
set an_old_dataset;
got_value = indexc(var_name,"Ads", "tc");
run;
Obs var_name got_value 1 AFHood 1 2 Analytics 1 3 Group 0 4 AAG 1 5 AFHood Analytics Group 1 Lastly is the indexw function. It will return the first occurrence of a word in the search string.data a_new_dataset; set an_old_dataset; got_value = indexc(var_name,"AFHood"); run;Obs var_name got_value 1 AFHood 1 2 Analytics 1 3 Group 0 4 AAG 1 5 AFHood Analytics Group 1 Summary: INDEX function - searches for patterns as separate words or as parts of words INDEXC function - searches for any characters that are present in the string INDEXW function - searches for strings that are words