AFHood Group Blog The thoughtless yammerings of marketing junkies..

30Dec/090

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.

11Oct/090

SAS arrays ( one dimensional )

A SAS array is a group of variables (or values) under a single name. The grouping is only temporary and only exists for the duration of the data step. There are many ways to declare an array. Additionally, arrays can be modified once they are created.

Here is one example of an array.

data new_ds;

set old_ds;

array test_array{5} test1 test2 test3 test4 test5;

do i=1 to 5;

sum_var+test_array{i};

end;

run;

Here are a few other ways to declare one-dimensional arrays:

Using the {*} designation allows SAS to determine the number of elements in the array.

array test_array{*} test1 test2 test3 test4 test5;

You can also use a range to specify values.

array test_array{5} test1-test5;

The first value doesn't have to be 1. You can specify the values if necessary.

array test_array{10:15} test1-test5;

You can also designate all the variables created into an array. NOTE: you cannot have both character and numeric variables in the same array.

array test_array{*} _NUMERIC_; ( All current numeric variables )

or

array test_array{*} _CHARACTER_; ( All current character variables )

or

array test_array{*} _ALL_; ( All current variables if they are of the same type )

There are also multi-dimensional arrays. We will discuss them in a later post.

If you need help working with arrays. We have an experience team of programmers available for short term help or contract engagements. Don't hesitate to contact us.

13May/0910

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.

5Mar/090

SAS Proc Contents… What is that data?

With the release of EG (Enterprise Guide) and analysts becoming more comfortable with a point and click environment, it's not as common that we use Proc Contents. This is another one of those tools that make up the basics in SAS. Yes, you can right click on a dataset in EG and see the 'properties' to get most of this data.

So here is an example:

PROC CONTENTS DATA=sashelp.dplog;
RUN;

In the output, you should first expect to see all of the dataset properties. Things like name, type, creation and modification dates, etc. Next is the engine information. This tells us how SAS and the dataset file are setup. Last we get the variables and attributes. You should see things like variable names, type, length, formats and labels (which can be very helpful and not overlooked in permanent datasets).

proc_contents

2Feb/093

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
14Jan/090

Character to Numeric conversions

This is one of those "must knows" for analysts dealing with multiple data sources or simply merging data on a regular basis.

The INPUT option converst from character to numeric. The syntax is as follows:

INPUT(variable or string, numeric informat);

The numeric informat tells SAS how to read the variable into the numeric format.  It is also important to note that you can't use the same variable name when changing the the variable type.  There is a way around that if it is absolutely necessary that the variable be named the same within this step, use the RENAME statement after the SET command.

Here are a couple of examples.

data test;

set test_in;

numeric=input(string,8.);

sasdate=input(date,mmddyy6.);

run;

To keep the same variable name:

data test;

set test_in (rename=(variable1=old1 variable2=old2));

old1=input(variable1,8.);

old2=input(variable2,mmddyy6.);

run;

Please let us know if you have any questions.

http://www.AFHood.com