第一:R加载/调用SAS
--在SAS中生成传送文件
LIBNAME SAS_R xport 'C:\sea.xpt';
DATA SAS_R.sea;
SET custdet1;
RUN;
--在R中读入
library(foreign)
library(Hmisc)
sea<-sasxport.get("c:/sea.xpt")
head(mydata)

第二:SAS调用R的数据
Library(foreign)
write.foreign(sea,"c:/sea.txt","c:/sea.sas",package="SAS")
在C盘中会生成二个文件:
SAS程序代码
一个是TXT文件
然后在SAS中直接加载程序就行。
 
install.packages("R.matlab")
library(R.matlab)
path <- system.file("mat-files", package="R.matlab")
mat <- readMat(file.path(path, "structLooped.mat"))
s <- mat$s
fields <- dimnames(s)[[1]]
cat("Field names: ", paste(fields, collapse=", "), "\n", sep="");

print(s)
 

It's Friday, and on SAS Voices they are posting fun stuff about dogs who work at SAS. I'm posting about PROC SQL and MySQL. You tell me - which of us knows how to ring in the weekend with style?

I've been working with MySQL data sources lately, and SAS/ACCESS to MySQL makes it easy to do my work in SAS. I can assign a SAS library and then work with the tables and columns almost exactly as if they lived in native SAS data sets. The database columns are automatically visible to my SAS program with SAS-style attributes, such as SAS formats and lengths.

But when I get started with a new database that I'm not familiar with, I'd like to know how MySQL sees that data, including the database-centric attributes. That information is available in the information_schema structure within the MySQL instance. (Note: other databases support the information_schema, so this program can go beyond just MySQL.)

/* must be run where SAS/ACCESS to MySQL available */
%let database=dbname; /* name of the MySQL database */
%let server = mysql.host.domain.com; /* your MySQL node */
 
proc sql;
  connect to mysql as source 
    (
    DATABASE=&database 
    SERVER=&server 
    PORT=3306 /* default port, change if needed */
    USER=your_user_id
    PASSWORD=secretPW
    );
  create table work.schematables as 
    select * from connection to source 
    (select * from 
      information_schema.tables 
    where table_schema="&database");
 
  create table work.schemacolumns as 
    select * from connection to source 
    (select * from 
      information_schema.columns 
    where table_schema="&database");
  disconnect from source;
 
quit;

After plugging my database information and credentials into the above program, and then running it, I receive two output tables. One table (WORK.SCHEMATABLES) shows me all of the database table attributes, such as the table row count, create/update times, index length, and more. I find the other table (WORK.SCHEMACOLUMNS) to be more interesting. It shows me the column names, intrinsic type (bigint, varchar, datetime, and so on), length, whether it's NULLible, and more.

Oh, and if you're a bit queasy about putting your database credentials into a SAS program like this, check out these five strategies to eliminate passwords from your SAS programs.

tags: MySQL, sas/access
 
SQL syntax contains a few set operators, such as UNION, EXCEPT and INTERSECT. The UNION operator concatenates the results of multiple SQL queries vertically into a single table for all matching rows, which I found particularly useful in PROC SQL while using SAS to manage data. Here come two examples.

Example 1 – Transpose data
PROC SQL can transform a dataset to any desired structure, without referring to DATA step or PROC TRANSPOSE. For example, SASHELP.CLASS can be transposed from wide to long by the UNION ALL clause, and reversely from long to wide by the MAX function and the GROUP clause.

From wide to long

data wide;
   set sashelp.class;
   drop sex;
run;

proc sql;
   create table long as
   select name, 'Age' as var, age as col1
   from wide
   union all
   select name, 'Weight' as var, weight as col1
   from wide
   union all
   select name, 'Height' as var, height as col1
   from wide
;quit;
From long to wide

proc sql;
   create table wide as
   select name,
      max(case when var = 'Age' then col1 end) as Age,
      max(case when var = 'Weight' then col1 end) as Weight,
      max(case when var = 'Height' then col1 end) as Height
   from long
   group by name;
quit;
Example 2 – Aggregate data into a cube
In SAS, PROC SQL doesn’t support the ROLLUP or CUBE clause. However, we can apply multiple UNION operators to simulate such functionality. For example, we can create a cube table to list all possible summations of the ACTUAL variable by STATE, PRODUCT, YEAR in the SASHELP.PRDSAL2 dataset. Afterward we can easily query this multi-dimensional data structure to look for interesting aggregation information, without running any other aggregating procedure again.

data prdsal2;
   set sashelp.prdsal2;
run;

proc sql;
   create table cube as
   select state,product, year, 'total by state, prodcut and year' as category,
   sum(actual) as actual
   from prdsal2
   group by state, product, year
   union
   select state, product, ., 'total by state and prodcuct', sum(actual)
   from prdsal2
   group by state, product
   union
   select state,'', year, 'total by state and year', sum(actual)
   from prdsal2
   group by state, year
   union
   select '',product, year, 'total by product and year', sum(actual)
   from prdsal2
   group by product, year
   union 
   select '' ,'', year, 'total by year', sum(actual)
   from prdsal2
   group by year
   union 
   select state, '',. , 'total by state', sum(actual)
   from prdsal2   
   group by state
   union 
   select '',  product, ., 'total by product', sum(actual)
   from prdsal2
   union
   select '', '', ., 'grand total', sum(actual)
   from prdsal2
   order by state, product, year
;quit;
 
This week's tip comes from SAS powerhouse Art Carpenter and his book Carpenter's Complete Guide to the SAS REPORT Procedure. SAS user Kim LeBouton called this book "the single best resource for PROC REPORT." And in his review, SAS user Charles Patridge said "This is a must-have book if you are a SAS [...]
 

There are no silver bullets that guarantee success for marketers. Betting on a single channel, technology, process or team is very much like betting everything on “Red” in a casino – there’s no more than a 50% chance of success – for each item.  Make bets on each, without understanding and aligning the relationships between channel, technology, process and team and the odds for success rapidly decline.

Helena Schwenk, Principal analyst at MWD advisors

Helena Schwenk, principal analyst at MWD advisors, a European IT advisory firm who consult with organizations to create tangible business improvements from IT investments, talked to us about the:

  • Capabilities
  • Issues
  • Technology and
  • Other considerations required to find profitable growth

She started with a focus on leadership, and then went on to discuss how issues will shift – from a single channel to multi-channel; from creating a picture of what’s happened to predicting what will happen in the future.

Helena walked us through the technology required – from data management to high end analytics and everything in between.

When focusing on other considerations, skills were top of her list – not just the skills required for analytics, but the changes that could ripple across your organization – wherever a staff member interacts with a customer.

Want to learn more?

Visit the driving profitable growth site on our Customer Intelligence Knowledge Exchange.

tags: customer analytics, customer intelligence, driving profitable growth
 
I’m Willie the Seeing Eye Dog. I help Software Development Manager Ed Summers get around SAS Corporate Headquarters without breaking his neck. The editors at SAS provided these questions for me to answer so you can learn more about what I do. What would SAS customers be surprised to learn about the [...]
 
I agreed to shoot at SAS Global Forum in Orlando some brief, YouTube-style videos of a few poster contest entrants.  I’ve been to the last four of these annual events, so I’m well aware of the extreme enthusiasm of the global community but the graciousness and excitement of these folks [...]
 

At SAS Global Forum, one of the most difficult areas for me to show to you are the poster presentations. This year, I asked Steve Polilli, a colleague who is great with a Flipcam, to video a few for me. I picked out three that I will use in the coming weeks on the Friday's Innovation Inspiration series.

The first in the series is a presentation by Jenine Milum from her paper, Creating a report in the SAS Information Delivery Portal using SAS Information Maps. To do a poster presentation, you still have to write a paper, but you also have to prepare a poster representation for display.  

 

 

Chris Battiston, a first-timer at SAS Global Forum 2012 and Second place awardee for Best Contributed Paper in the Posters category for his paper, Wake Up Your Data with Graph'n'Go, says that poster presentations may be a great way to gradually learn speaking skills for those who are uncomfortable speaking in front of a large audience. Your presentation is only a few minutes - unless Polilli gets a hold of you.

Be sure to check out Milum's award-winning Coders' Corner Paper. She won Best Contributed Paper for Proc Format, a Speedy Alternative to Sort / Sort / Merge.

tags: Friday's Innovation Inspiration, papers & presentations, SAS Global Forum
 


*** if VAR is missing, sas will drop that records when calculating mean. To include that records, we can impute that missing var by 0 ***;

data test;
input a $ 1-3 b 4-5 ;
cards;
a 1
a
a 3
b
b 2
b 1
c 1
c
1
4
;
run;

proc print data=test noobs;
title "print of original data";
run;

proc summary data=test nway missing;
var b;
class a;
output out=sum1(drop=_type_ _freq_) mean=;
run;

proc print data=sum1;
title "Summary with MISSING opinion";
run;

proc summary data=test nway;
var b;
class a;
output out=sum2(drop=_type_ _freq_) mean=;
run;

proc print data=sum2;
title "Summary without MISSING opinion";
run;

proc means data=test nway missing;
var b;
class a;
output out=means1(drop=_type_ _freq_) mean=;
run;

proc print data=means1;
title "Means with MISSING opinion";
run;

proc summary data=test nway;
var b;
class a;
output out=means2(drop=_type_ _freq_) mean=;
run;

proc print data=means2;
title "Means without MISSING opinion";
run;





^LSummary with MISSING opinion

Obs a b

1 2.5
2 a 2.0
3 b 1.5
4 c 1.0


^LSummary without MISSING opinion

Obs a b

1 a 2.0
2 b 1.5
3 c 1.0


^LMeans with MISSING opinion

Obs a b

1 2.5
2 a 2.0
3 b 1.5
4 c 1.0


^LMeans without MISSING opinion

Obs a b

1 a 2.0
2 b 1.5
3 c 1.0