AFHood Group Blog The thoughtless yammerings of marketing junkies..

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

“call symputX” is the symput upgrade

SymputX is the upgrade from Call Symput.

Syntax:

call symputx("macro_var_name", character_value (or numeric to be converted to char), symbol table def);

Where symput will produce a note about converting character values to numeric, symputx won't produce such a note. Additionally it will strip leading and trailing spaces form the character value. Lastly, you can define which symbol table should be used (G=global, L=local, F=operates like symput).

data test;

set test_old;

call symput('variable_name', 'numeric value') /* gets a warning although it runs successfully*/;

call symputx('xvariable_name', 'numeric value') /* no warning for numeric conversion*/;

run;

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;

18Sep/091

Using SELECT groups for conditional processing

What are Select Groups?

Select Groups are another way SAS enables conditional processing of datasets.

When should we use Select Groups instead of if-then statements?

According to our friends at SAS, you should utilize Select Groups when your conditional criteria is mutually exclusive and numeric. When this is the case, Select Groups are more efficient than if-then processing.

Here are a few examples of conditional processing using Select Groups:

data NEW_DS;

set DS1;

select var1;

when (1) var2='YES';

when (2) var2='NO';

otherwise var2='UNKNOWN';

end;

run;

data NEW_DS;

set DS1;

select;

when (var1=1 and var2='JAN') var3='YES';

when (var1=2 and var2='FEB') var3='NO';

otherwise var3='UNKNOWN';

end;

run;

20Feb/090

SAS Conditional programming – If Then

One of the great things about SAS is the use of Macros and conditional programming. The ability to split the code using If - Then processing allows one piece of code to accomplish many things. Here is the basic syntax for If - Then statements in a SAS macro.

%let error1=;

%if %length(&error1) ~=0 %then

%put WARNING: There is an error!;

%else

%put NOTE: There is no error.;

This is the basic example where you want to process one command given a condition. Lets say that you want to process a whole section of code depending on a single condition. See below:

%let error1=;

%if %length(&error1) ~=0 %then %do;

%put WARNING: There is an error!;

<and any other code you want to run here>

%end;

%else

%put NOTE: There is no error.;

<and any other code you want to run here>

%end;

Note that the %do command allows you to run a whole block of code. Be sure to include the %end statement at the end of the code block. Also, the %else portion of both examples are completely optional.