sas >> density function (I wanted to add an attachment- but couldn't

by rhigh » Tue, 09 Jun 2009 21:46:40 GMT

Tal,

I ran the following normal data through the macro and drew a density ..
you can adjust the size and position of the graph with options on the axis
statements.

data indat;
do i = 1 to 500; y = rannor(1929); output; end;
run;

GOPTIONS reset=all gunit=pct FTEXT=swissb ROTATE=landscape
htitle=3 htext=3 CBACK=white ctext=black PAPERSIZE=(11,8.5);

axis1 label=(angle=90) length=6.5 in order=(0 to .5 by 0.1) label=(a=90
'Density') value=(h=2) minor=none;
axis2 order=(-4 to 4 by 0.5) length=8 in offset=(.5 cm) value=(h=1.5)
minor=none origin=(1.5 in, 1.5 in);

pattern1 v=solid c=grey;
symbol1 v=none i=join c=black;

* I eliminated the call to boxaxis, since not found in this macro;

%*boxaxis(data=data, out=boxanno, var=NOX, pos=96, boxwidth=5);

%density(data=indat, var=y,
out=densplot111,
vaxis=axis1,
haxis=axis2,
symbol=symbol1,
window=.7, /* Bandwidth (H) -- determines how smooth the density
is plotted (smaller ==> more jagged ) */
showobs=YES, /* = NO to remove small vertical bars at bottom */
plotopt=%str(areas=0 href=0 lhref=1)); * area=1 --> fill with
color;

The data plotted on the graph is in the out= dataset.


Robin High
UNMC








tal < XXXX@XXXXX.COM >
Sent by: "SAS(r) Discussion" < XXXX@XXXXX.COM >
06/09/2009 07:56 AM
Please respond to
tal < XXXX@XXXXX.COM >


To
XXXX@XXXXX.COM
cc

Subject
density function (I wanted to add an attachment- but couldn't figure out
how, can I send a post to the group's mail address? what is
is?)






Hi all,
I'm trying to plot the density function of several variables in my
data set.
I found a macro which I dont understand completely, and I tried to use
it.
1- Can I count on the density out file that it's the density function
(when i plot it with G Plot at the end)?
2- The grap I get is very small comparing to the window. I tried to
change the window parametter ,but it didn't help. I also get Black
lines that I don't need- how do i get rid of them?

/*-------------------------------------------------------------------*

* Name: density.sas *

* Title: Nonparametric density estimates from a sample. *

* Doc: http://www.math.yorku.ca/SCS/sssg/density.html *

* *

* User chooses a bandwidth parameter to balance smoothness and bias *

* and the range of the data over which the density is to be fit. *

*-------------------------------------------------------------------*

* Author: Michael Friendly < XXXX@XXXXX.COM > *

* Created: 23 Mar 1989 16:21:12 *

* Revised: 28 May 2004 16:21:271 *

* Version: 1.1 *

* -handle special missing values (.A-.Z) *

* -fixed variable names for V7+ (thx: Dietrich Alte) *

* From ``SAS System for Statistical Graphics, First Edition'' *

* Copyright(c) 1991 by SAS Institute Inc., Cary, NC, USA *

* *

*-------------------------------------------------------------------*

* Original program by: C. ROGER LONGBOTHAM *

* while at Rockwell International, Rocky Flats Plant *

* From: SAS SUGI 12, 1987, 907-909. *

*-------------------------------------------------------------------*/


%macro density(

data=_LAST_, /* Name of input data set */

out=DENSPLOT, /* Name of output data set */

var=X, /* Input variable (numeric) */

window=20, /* Bandwidth (H) */

xfirst=., /* . or any real; smallest X value */

xlast=., /* . or any real; largest X value */

xinc=., /* . or value>0; X-value increment */

/* Default: (XLAST-XFIRST)/60 */

gplot=YES,

symbol=,

haxis=, /* AXIS statement for horizontal axis */

vaxis=, /* and for vertical axis- use to equate axes */

plotopt=, /* other plot options, eg %str(areas=1) to fill */

anno=,

showobs=YES,

name=density

);

%*-- Remove missing data;

%local abort;

%let abort=0;

data _in_;

set &data;

keep &var;

if &var > .Z;

run;

%if &syserr > 4 %then %let abort=1; %if &abort %then %goto DONE;

%*-- Get the variable label;

proc contents data=_in_ out=_work_ noprint;

%if &syserr > 4 %then %let abort=1; %if &abort %then %goto DONE;

data _null_;

set _work_(keep=name type label);

if upcase(name) = upcase("&var") then do;

if label=' ' then label="&var";

call symput('vlabel', label);

end;

run;

proc sort data=_in_;

by &var;


proc iml;


start WINDOW; *-- Calculate default window width;

mean = xa[+,]/n;

css = ssq(xa - mean);

stddev = sqrt(css/(n-1));

q1 = floor(((n+3)/4) || ((n+6)/4));

q1 = (xa[q1,]) [+,]/2;

q3 = ceil(((3*n+1)/4) || ((3*n-2)/4));

q3 = (xa[q3,]) [+,]/2;

quartsig = (q3 - q1)/1.349;

h = .9*min(stddev,quartsig) * n##(-.2); * Silvermans formula;

file log;

put 'NOTE: Using calculated default window width, window=' h;

finish;


start INITIAL; *-- Translate parameter options;

if xf=. then xf=xa[1,1];

if xl=. then xl=xa[n,1];

if xl <= xf then do;

file log;

put 'ERROR: Either largest X value chosen is too small';

put ' or all data values are the same';

stop;

end;

if dx=. | dx <= 0 then do;

inc = (xl-xf)/60;

rinc = 10 ## (floor(log10(inc))-1);

dx = round(inc,rinc);

end;

if xf=xa[1,1] then xf=xf-dx;

nx = int((xl-xf)/dx) + 3;

finish;


*-- calculate density at specified x values;

start DENSITY;

fnx = j(nx,3,0);

vars = {"DENSITY" "&VAR" "WINDOW"};

create &out from fnx [colname=vars];

sigmasqr = .32653; * scale constant for kernel ;

gconst = sqrt(2*3.14159*sigmasqr);

nuh = n*h;

x = xf - dx;

do i = 1 to nx;

x = x + dx;

y = (j(n,1,x) - xa)/h;

ky = exp(-.5*y#y / sigmasqr) / gconst; * Gaussian kernel;

fnx[i,1] = sum(ky)/(nuh);

fnx[i,2] = x;

end;

fnx[,3] = round(h,.001);

append from fnx;

finish;


*-- Main routine ;

use _in_;

read all var "&var" into xa [colname=invar];

n = nrow(xa);

%if &window=%str() %then %do;

run window;

%end;

%else %do;

h = &window ;

%end;


xf = &xfirst;

xl = &xlast;

dx = &xinc;

run initial;

run density;

close &out;

quit;

%*-- Restore variable label;

data &out;

label &var = "&vlabel";

set &out;


%if &gplot = YES %then %do;

%if %length(&symbol)=0 %then %do;

symbol1 v=star i=join c=black;

%end;

%let annoopt=;

%if %length(&anno) > 0 %then %let annoopt = anno=&anno;


%if &showobs = YES %then %do;

data annoobs;

set &data;

retain xsys '2' ysys '1' when 'A';

x = &var;

y = 0; function='MOVE '; output;

y = 3; function='DRAW '; output;

%let annoopt=anno=annoobs;

%if %length(&anno) > 0 %then %do;

data annoobs;

set annoobs &anno;

when='A';

%end;

%end;


proc gplot data=&out ;

plot density * &var / frame &plotopt &annoopt

name="&name"

des="Density plot of &var"

%if %length(&haxis)>0 %then %do;

haxis = &haxis

%end;

%if %length(&vaxis)>0 %then %do;

vaxis = &vaxis

%end;

;

run ;

%end;

%done:

%mend;









axis1 order=(0 to .5 by 0.01) label=(a=90 'Density')

offset=(3,)

;

pattern1 v=solid c=red;

symbol1 v=none i=join c=black;

%boxaxis(data=data.data, out=boxanno, var=NOX, pos=96, boxwidth=5);

%density(data=data.data, var=O3, out=densplot111,vaxis=axis1,

haxis=axis2,

symbol=symbol1,

anno=boxanno,

plotopt=%str(areas=1));





proc gplot data=densplot111;

plot density * O3;

symbol1 i=join v=none;


Similar Threads

1. a question of kernel density function

Hello SAS experts,



My question, in brief, is: is there a SAS procedure that can fit kernel density function, with which the kernels have finite support?





Specifically, I want to generate a kernel density function for a variable based on some realized values (the realized values are all positive). I find that SAS procedure PROC KDE can do that using Gaussian density. This somewhat concerns me, since theoretically Gaussian densities allows for negative or very high positive realization of the variable. I want the kernels having finite support, which is nonnegative and not unrealistic high. Does PROC KDE has some options to ensure the finite support? Or is it possible that I can change Gaussian kernel to others, such as Quartic kernel or Epanechniko?



---------------------------------
 Yahoo! FareChase - Search multiple travel sites in one click.

2. drawing density function

3. truncated attachment name from MVS

Folks,
when I attach a GDG in a SAS e-mail, the attachment name gets
truncated to -0.txt in the received mail.  Similarly when attaching a
long MVS filename, e.g xxxx.yyyyy.zzzzz.report.file, the received
attachment name is truncated to report.file

Is there an option I'm missing?

Sample Code:
FILENAME SASCODE "ZTRS.INCLUDE.PARM" DISP = SHR WAIT = 5;

FILENAME MYMAIL EMAIL;

DATA _NULL_ ;
     FILE MYMAIL;
     INFILE SASCODE(CBBMAIL) EOF = LASTOBS;
     LENGTH NAME $30.;
     INPUT NAME;
     FIRSTNAME = SUBSTR( NAME, 1, INDEX( NAME, "@" ) );
     %INCLUDE SASCODE( MAILFROM );
     PUT '!EM_TO! "' NAME '"' ;
     PUT "!EM_SUBJECT!MONTHLY REPORT";
     PUT NAME ",";
     PUT "    SEE ATTACHED FILE";
     PUT "!EM_ATTACH!('ZXXX.XXXXXXX.XXXXXXX.DATA(-0)')";
     PUT "!EM_SEND!";
     PUT "!EM_NEWMSG!";
  RETURN;
LASTOBS: PUT "!EM_ABORT!";


Received mail has correct attachment, but has attachment name
-0.txt

Thanks, any help gratefully received!

4. density function (I wanted to add an attachment- but couldn't figure out how, can I send a post to the group's mail address? what is is?)

5. Email Attachment Problems

I have created an Excel file using the following PROC EXPORT code:

PROC EXPORT DATA= &LbNm..XIBNRClaims_Summ
            OUTFILE= "&ExcelDir./IBNR_Claims.xls"
            DBMS=xls REPLACE;
RUN;

Now, I'm trying to e-mail the Excel file to myself using this code:


FILENAME Mailbox EMAIL EMAILSYS=SMTP;

DATA _NULL_ ;
	FILE Mailbox TO = &EmailAdd.
		SUBJECT = 'IBNR Claims File';


	PUT '!EM_ATTACH! ("&ExcelDir./IBNR_Claims.xls" name="IBNR"
ext="xls")' ;

	PUT "Hello Dave.";
	PUT "My name is HAL.SAS.9.1.3";
	PUT "I have finished your data requests.";
	PUT "The IBNR Claims Excel File is attached.";

RUN;

Everything runs fine and SAS sends the e-mail to me with the
attachment.  However, when I try to open the attachment, Excel gives
me some crazy error.  HOWEVER, if I FTP the file over, Excel has no
problem opening it.  I've even tried the above code without setting
the name and ext options.

My question is this.  Does the SAS e-mail process do something to the
file that FTP does not?  Is there some additional option that I need
to set within my code or something that I'm missing?

Thanks!

6. SAS e-mail Attachment Errors

7. Inbound attachment usage restricted by BP policy (Replaced with text)

This is a multi-part message in MIME format.

************* eManager Notification **************

BP Security Policy restricts the transmission of certain attachment types. If you are sending a legitimate file please resend this in a ZIP file.

Source mailbox: " XXXX@XXXXX.COM "
Destination mailbox(es):  XXXX@XXXXX.COM 
Policy: Replaced with text
Attachment file name: your_details.zip - application/x-zip-compressed
Action: Attachment Removal

******************* End of message *******************

8. density function (I wanted to add an attachment- but couldn't figure out how, can I send a post to the group's mail address? what is is?)