Wednesday, 6 March 2013

Concatenation of two data fields in SAS EG

I often need to concatenate two fields, let's call them name_1 and name_2, into one field - Ful_name. I'm not entirely sure whether one or both of these fields has leading or trailing spaces.

My first attempt:

Strip(Name_1) || " " || Strip(Name_2)

works fine. I tested this code today and 7 million rows processed in 0.2 seconds.

My second attempt uses the CATX function. This takes the format:

CATX(delimiter, first_string, second_string, third_string etc)

So I used, CATX(' ', Name_1, Name_2)

CATX removes leading and trailing blanks from the specified strings and then concatenate these results using the specified delimiter - here a space character.

This version processed the 7 million rows in 0.1 seconds, a 50% improvement.

SAS built in functions will almost always work more efficiently than user designed versions, so it's worth studying the SAS functions!

There's documentation about CATX here.



Tuesday, 5 March 2013

My first posting

I work in HMRC, the UK tax authority, as a data analyst. My main analytical tool is SAS Enterprise guide 4.2. This blog is intended to record and share useful SAS snippets or techniques I come across in my work. There will be no tax specific data here, I promise!