AFHood Group Blog The thoughtless yammerings of marketing junkies..

31Mar/110

Making SOAP calls from SAS! Integrating with web services

One of the issues with SAS for many IT departments is the lack of integration with service oriented architecture (SOA). The good news is with many new features coming online with versions 9.X+ are service oriented.

Lets look quickly at the Proc SOAP procedure now available.

For those SAS programmers out there that aren't familiar with SOAP or services, get your basis here: SOAPUser-Basics

In a nutshell, SOAP is transporting XML data via a HTTP Post. In order to make a successful SOAP call from SAS you need a couple of things.

1. a request XML file

2. a repsonse XML file

3. a webservice URL and WSDL (Web Service Definition Language) -Think webservice users manual

Here is a simple example of a SOAP call we use on a daily job.

filename rqst_xml 'some file system reference';

* Create the XML;

data _null_;

set input_dataset;

file rqst_xml;

if first.records=1 then do;

put '<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
<requestingSystem>SAS</requestingSystem>

<requestingFunction>DemoSasScript</requestingFunction>
</soap:Header>

<soap:Body>
<requestedData>';

end;

datasetData;

if last.records=1 then do;

put '</requestedData>
</soap:Body>

</soap:Envelope>';

end;

run;

filename rspns_xml 'some file system reference';

%let URL=http://webservice_url/service;

proc soap in=rqst_xml

out=rspns_xml

url=&url;

run;

 

Upon executing the call, you can read in the rspns_xml data with the SAS XML engine.

This is meant to be a simple example with very limited scope. Service architecture can quickly get complicated with error and condition handling. Please let us know if you need help with your SAS architecture or coding.

12Oct/100

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
9Aug/102

SAS: Where Also

Ever heard of 'where also'? Neither did we.

We have to give credit to the guys at the SAS Community.

'Where also' allows you to add a series of where statements. The use acts like a single where statement with the and condition.

Example:

Data new_data;

set old_data;

where number=6;

where also another_number=.;

run;

This is the same as where number=6 and another_number=.

19Jul/100

Sending an email from SAS

Have you ever wanted to know when your code completes? Or maybe you want to automate the report to include sending an email?

Regardless, SAS is happy to send that email for you. The easiest way to do this is through the SMTP access method via filename statements.

Example:

filename sendemail email 'toaddress@email.com' subject='This is a test email.' from='fromaddress@email.com';

data _null_;

file sendemail;

put 'Hi,';

put 'This is a email sent from SAS';

run;

This is only a simple example. Here is another with an attachment.

filename sendemail email 'toaddress@email.com' subject='This is a test email.' from='fromaddress@email.com' attach='/somesascode.sas';

data _null_;

file sendemail;

put 'Hi,';

put 'This is a email sent from SAS';

run;

Email attributes can also be set in the data step through the EM_ directives.

filename sendemail email 'toaddress@email.com' ;

data _null_;

set somedataset;

put '!EM_TO! ' email_addr;

put '!EM_SUBJECT! ' subscription_name;

put name ' ,';

put 'This message was generated by a SAS data set';

put '!EM_SEND!';

run;

3Dec/090

SAS INTNX function ( add days, months, years, etc )

The INTNX function increments dates, times, or datetimes by specific or custom intervals.

Here is the basic syntax for INTNX:

INTNX(interval[multiple.shift-index], start, increment[, alignment])

The interval can be a set value like - DAY, WEEK, DTWEEK, YEAR, etc. You can also specify a custom interval. We will post more on that later.

The multiple is optional and used as such. If you want the interval to be a 2 year period or biennial, you could use YEAR2. It is the same with weeks. If you were calculating 2 week pay periods and you wanted to increment by such, you would declare the interval to be WEEK2.

The shift-index lets you define a starting point within the interval. An example would be to start the week on Monday.  The interval designation should be WEEK1.2 .

The start is your date, time, or datetime at which you want to begin the increment.

Increment is a positive or negative integer representing the number of interval shifts that should be made from the start.

Alignment is an excellent tool. It allows you to designate at which point SAS should return within an interval. You can specify:

B - beginning

M - middle

E - end

S - same

The default for alignment is beginning.

29Oct/090

SAS DIM function – Counting the elements in an array

The DIM function returns the number of literal elements in an array. It functions against multi-dimensional arrays as well as one-dimensional arrays.

1-dimensional array example

DIM(array_name)

Multi-dimensional array examples

DIM(m_array) -> returns the number of elements in the first dimension of the array

DIM5(m_array) -> returns the number of elements in the 5th dimension of the array

DIM(m_array, 5) -> returns the number of elements in the 5th dimension of the array

The classic use case for the DIM function is to return the number of elements in an array for the upper bound of a do loop process. Example:

array array_name(5) var1 var2 var3 var4 var5;

do i=1 to dim(array_name);

some SAS statements here

end;

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.

11Oct/090

Detecting the end of a SAS data set ( end= )

The END= option can help you determine when you have reached the last record in your dataset. Here is a syntax example:

data new_ds;

set old_ds end=end_var;

if end_var=1 then text='This is the last variable';

run;

The end= option creates a temporary variable, in the example it is end_var. This variable is not written to the output dataset. The variable is numeric and has a value of 1 to specify the last record.

Do not use this option with the point option.

This option is helpful when you only want to output the last record (good for summing data).

data new_ds (drop=transactional_record);

set old_ds end=last_record;

sum_var+transactional_record;

if last_record=1;

run;

7Oct/090

SAS Do loops – Do While

Here is yet another Do Loop post.

The basic form of a do loop is as follows:

Data new_ds;

Set old_ds;

Do some_index_var= 1 to 50 by 1;

Some SAS statements go here;

End;

run;

However, specifically in this post we want to talk about DO WHILE loops. Do while loops are different because they evaluate the condition at the top of the loop. Example:

Data new_ds;

Set old_ds;

Do while var > 100;

Some SAS statements;

End;

Run;

This loop will not execute the loop if the condition is not met upon the first execution. This is what makes WHILE so valuable. Conditional processes that allows you to exit the loop before the first iteration.

25Sep/090

SAS Do Loop – Do Until

Do loops are great for a long list of tasks. Understanding do loops can save you an innumerable amount of programming and work arounds.

The basic form of a do loop is as follows:

Data new_ds;

Set old_ds;

Do some_index_var= 1 to 50 by 1;

Some SAS statements go here;

End;

run;

However, specifically in this post we want to talk about DO UNTIL loops. Do until loops are different because they evaluate the condition at the bottom of the loop. Example:

Data new_ds;

Set old_ds;

Do until var > 100;

Some SAS statements;

End;

Run;

This loop will execute the SAS statements at least one time and evaluate the var>100 condition at the bottom of the loop. If the condition is false, the loop will execute again.