Archive for the ‘SAS Code’ Category

Export SAS data into XML File

Samstag, April 26th, 2008

If you need to export SAS data into a XML file than you have several options.
One possibility is a manual export via datastep.
Please take into account that the XML „child“ data must be stored horizontal in the SAS table.
You can access this SAS „child“ data later by using n-dimensional arrays.

The following example shows how it works.
The „+(-1)“ is a pointer to avoid blanks in the final XML file.

filename outxml „[yourPath]“;
data _null_;
 file outxml;
 set input NOBS=Lst;
by custid;

%let tab=“ „;

array var_a{3};
array var_b{3, 8} var_b1_1-var_b1_8
                  var_b2_1-var_b2_8
                  var_b3_1-var_b3_8;

if _n_=1 then do;
put ‚<?xml version=“1.0″ encoding=“windows-1252″?>‘;
put ‚<tns:DATAFILE Producer=“SAS“ ProductionTime=“‚;
put ‚ xmlns:tns=“http://…“‚;
put ‚ xmlns:xsi=“http://…“‚;
put ‚ xsi:schemaLocation=“http://…“>‘;
end;

put;
put ‚ <tns:Update…Record>‘;
put ‚ <tns:CustomerID>‘ custid +(-1) ‚</tns:CustomerID>‘;
do i=1 to 3 by 1;
 if var_a{i} ne . then do;
  put ‚ <tns:AccountData DateOfInfo=today‘;
  put ‚ <tns:AccountNumber>‘ var_a{i} +(-1) ‚</tns:AccountNumber>‘;
  put ‚ <tns:AccountType>F</tns:AccountType>‘;
   do m = 1 to 8;
    if var_b{i, m} ne . then do;
     put ‚ <tns:CardNumber>‘ var_b{i, m} +(-1) ‚</tns:CardNumber>‘;
    end;
   end;
    put ‚ </tns:AccountData‘;
  end;
 end;
 put ‚ </tns:Update…Record>‘;
run;

Import external data or files into SAS using wildcards

Sonntag, November 18th, 2007

If you want to import all files of a folder with a specific name or type you can use wildcards to store them in a SAS dataset.
You can use this example:

/*
this datastep will import all
ASC files of the specified folder
*/
data WORK.share;
infile ‚D:\yourPath\*.asc‘
delimiter=’09’x
MISSOVER
DSD
lrecl=32767 ;
format date mmddyy10. ;
informat date mmddyy10. ;
input
date
;
run;

SAS Enterprise Guide 4.x & Autoexec.sas

Donnerstag, November 8th, 2007

If you have problems using the autoexec.sas in combination of SAS Enterpise Guide you can use my instructions:

1. run Enterprise Guide
2. Click on Tools
3. Click on Enterprise Guide Explorer
4. In the Folder „Servers“ right click on the SAS Server of your choice
5. If possible click on „Disconnect“ otherwise click on „Properties“
6. Click on Options then you can type in a „SAS startup statement“
7. Here you type in the startup instruction for your autoexec.sas.
For Example:
%include ‚/path/autoexec.sas‘;
Don’t forget the semicolon!

Sample Size Calculation

Sonntag, Februar 25th, 2007

*
SAMPLE SIZE CALCULATION
How many customer should we randomly select to get significant results?
– Customer Base: 10.000 customer
– Standard Deviation calculated by PROC MEANS
– Probability value: 5%
– Margin of Error: 1 Euro

Detailed Documentation:
 http://www.isixsigma.com/library/content/c000709a.asp

Simulation of Customer Base Table:
 acct_num = Accountnumber
 fee = monthly fee in Euro per customer
;

data customer_base(drop=i);
do i=1 to 10000 by 1;
 acct_num=i;
 fee=
100 + round(25*ranuni(1),1);
 output;
end;
run;

proc means data=customer_base;
 
var fee;
 
title ‚Descriptive Statistics using Proc Means‘;
 
output out=customer_statistics;
run;

*store Standard Deviation in macro variable for later calculations: ;
proc sql noprint;
 select fee into :std
 from customer_statistics
 where _stat_=‚STD‘
;
quit;

*
Calculation of the sample size:
sample_size = (critical value Z alpha/2 * Standard Deviation of the Population) / margin of the Error)**2
Z alpha/2 is calculated with the Table of Standard Normal Distribution
;
data sample_size;
 sample_size=round(((
1.96 * &std)/1)**2,1);
run;
* In this example a Minimum of 200 customers is needed to get significant results;
 

 

SAS: Contour Plot with PROC GCONTOUR

Samstag, Januar 20th, 2007

Contour PlotTo create interesting 3D Graphics which are easy to understand you can use PROC GCOUNTOUR.
With this procedure you are able to create graphics for your next presentation or for playful marketers.

 

The following Code and the picture are an example how to create Contour Plots:

data surface;
do i=1 to 50 by 1;
x=i;
do k=1 to 50 by 1;
x=x;
y=k;
z=round(sqrt(
5*y*(x**2)+3),1);
output;
end;
end;
run;

GOPTIONS xpixels=800 ypixels=600;
GOPTIONS CBACK=WHITE;

LEGEND1
LABEL=(FONT=‚Arial‘ HEIGHT=8pt JUSTIFY=CENTER ‚Revenue:‘ )
POSITION=(MIDDLE BOTTOM OUTSIDE)
across=4
down=1
value=(tick=1 height=5.5pt JUSTIFY=LEFT ‚Low‘
tick=2 height=5.5pt JUSTIFY=LEFT ‚Low – Medium‘
tick=3 height=5.5pt JUSTIFY=LEFT ‚Medium – High‘
tick=4 height=5.5pt JUSTIFY=LEFT ‚High‘)
CSHADOW=GRAY
CFRAME=CXE8E8E8
CBORDER=GRAY;

TITLE ‚Contour Plot‘;
TITLE2 ‚PLOT: x * y * sqrt(5*y*(x**2)+3)‘;

PROC GCONTOUR DATA = surface;
PLOT x * y = z /
PATTERN
LEGEND=LEGEND1
NLEVELS=4
SMOOTH
CFRAME=WHITE
CLEVELS=GREEN YELLOW ORANGE RED
;
RUN;
QUIT;
TITLE; TITLE2; FOOTNOTE;
     

SAS: Using GOTO Statement in Macro Steps

Montag, Januar 15th, 2007

It is not perfect to use the GOTO statement in SAS Code. But if needed you can use the following macro structure:

%macro goto_struct(var);
%IF %QUOTE(&var) NE 16444 %THEN %DO;
%GOTO error; %
%error:
data error;
x=1;
output;
run;
%END;

%IF %QUOTE(&var) EQ 16444 %THEN %DO;
%GOTO ok; %
%ok:
data ok;
x=2;
output;
run;
%END;
%mend goto_struct;
%goto_struct(
100);
 

Using email functions with SAS

Freitag, Januar 12th, 2007

SAS provides several possibilities to send out emails. This is a good feature to get statusinformation while a SAS-program is running.

1st – sending emails with SAS/Macro (Author: DeVenezia.com):

%macro quikmail (to,subj,body);
%if (%superq(to) eq ) %then %let to=foobar;
%if (%superq(to) ne and %superq(subj) ne and %superq(body) ne) %then %do;
filename quikmail email „%superq(to)“ subject=„%superq(subj)“;
data _null_;
file quikmail;
put
„%superq(subj)“;
%let body=%left(%superq(body));
%do %until (%superq(body) eq);
%let p=%index(%superq(body),\n);
%if (&p eq 0) %then %do;
put
„%superq(body)“;
%let body=;
%end;
%else %do;
put
„%substr(%superq(body),1,%eval(&p-1))“;
%if (%eval(&p+2) gt %length(%superq(body))) %then
%let body = ;
%else
%let body = %substr (%superq(body),%eval(&p+2));
%end;
%end;
run;
%end;
%mend quikmail;
%quikmail(MyEmail@provider.com, Subject , MyText);

2nd – sending emails with Datastep:

data _null_;
filename mail email
to = (MyEmailAdress)
subject =
„SAS Mailtest“
/*attach = („Path and Filename 1,Path and Filename 2“)*/;
file mail;
put „Zeile 1“;
put „Zeile 2“;
run;

SAS: Import and Export Data

Donnerstag, Januar 11th, 2007

If you want to import data into SAS with SAS Code you can use the following structure:

data my_table;
infile „[Type in the full Path where to find the data]“
delimiter=‚,‘
missover
dsd
lrecl
=32767
firstobs=2
;
length
first_field $ 24
;
input
first_field $
;
label
first_field = „first_field“
;
run;

If you want to export data you can use the following structure. If you need more detailed code you can use the procedure output in the LOG window.

proc export data=my_table
outfile=„[Type in the full Path where to find the data]“
dbms=dlm;
delimiter=‚|‘;
run;    

SAS: SQL Pass Through

Mittwoch, Januar 10th, 2007

With this SAS Program you are able to connect to a database with SQL Pass Through via ODBC. The advantage is, that the SQL Statement will be processed by the resources of the database. The disadvantage is, that you cannot use SAS specific functions which are not supported by the database sql language.

proc sql;
connect to odbc (dsn=“…“ user=“…“ password=“*****“);
create table my_table as
select * from connection to odbc
(
select

from

where

);
disconnect from odbc;
quit;