Google SAS Search

Add to Google

Friday, September 30, 2005

SAS Function Friday

In an attempt to increase my blog posting rate I will be highlighting a SAS function every (hopefully) Friday. At least ever Friday that I have access to a computer.

There's so many SAS functions, where do we start? Do we go for one of the more obscure ones in order to start things off with a little razzle dazzle? Something like CALL PEEK or CALL POKE. Cause it's always useful to know the address of your SAS variable. Actually, it can be very useful, just not very often. How about something a little more pedestrian?

Everybody knows all about the COMPRESS() function, right? It's not very razzle dazzle but it's darn useful. Compress() can be used to get rid of......
specific CHARACTERS in a character string!

Notice I did not say spaces. Although, a lot of times it's used just to get rid of spaces. That's the default for the optional second parameter.


data _null_;
x = 'get rid of spaces';
x = compress( x );

y = 'get_rid of spaces_and_ underscores';
y = compress( y, ' _' );
put x = ;
put y = ;
run;

One question you should always have when approaching a SAS character function is what is the length of the return value? In other words, if you create a new variable, what is it's length going to be?

data _null_;
x = "Woohoo! It's Friday!";
y = compress( x );
run;

What is the length of y? Eight (the "default" for a SAS variable)? Eighteen (the length of Woohoo!It'sFriday with the spaces comressed out)? Twenty (the length of the variable x)?

Of course you knew the answer is 20. Woohoo! smart reader.

2 comments:

  1. Wrong answer. Length of y = 18. Length of x = 20.

    ReplyDelete
  2. Actually, the length of y is 20. This gets set during compile time. The compiler doesn't get any information from the compress() function because the function hasn't executed yet. This can be easily confirmed by creating a work data set instead of using _null_:
    data test;
    x = "Woohoo! It's Friday!";
    y = compress( x );
    run;
    proc contents data = test; run;

    You will see x and y both have a length of 20.

    Now, if you used the length() function to look at the value of y you will get 18. But that's the length of the value, not the variable. I should have worded my original post better to ask specifically what the length of the VARIABLE y is.

    Thanks for your comment! -s

    ReplyDelete