Statistical programming, matrix languages, and more

Send this IML output to dataset instead of result window

Accepted Solution Solved
Reply
Highlighted
Contributor
Posts: 31
Accepted Solution

Send this IML output to dataset instead of result window

This is either impossible or really easy, but today's the first day I've ever looked at IML.  I am using this code to analyze a dataset to see what percent is missing for each variable.  I need the results in a dataset instead of a result window.

 

proc iml;
use one;
read all var _NUM_ into x[colname=nNames]; 
n = countn(x,"col");
nmiss = countmiss(x,"col");
 
read all var _CHAR_ into x[colname=cNames]; 
close one;
c = countn(x,"col");
cmiss = countmiss(x,"col");
 
/* combine results for num and char into a single table */
Names = cNames || nNames;
rNames = {"    Missing", "Not Missing"};
cnt = (cmiss // c) || (nmiss // n);
print cnt[r=rNames c=Names label=""];
quit;

Accepted Solutions
Solution
a week ago
Contributor
Posts: 31

Re: Send this IML output to dataset instead of result window

I figured it out on my own.  Posting this for anyone who finds this post later.  This would work to analyze a set to find missing rates of all variables, but it runs out of memory for my large data.

 

proc iml;
use one;
read all var _NUM_ into x[colname=nNames]; 
n = countn(x,"col");
nmiss = countmiss(x,"col");
 
read all var _CHAR_ into x[colname=cNames]; 
close one;
c = countn(x,"col");
cmiss = countmiss(x,"col");
 
/* combine results for num and char into a single table */
Names = cNames || nNames;
VarName = {"Missing", "Not Missing"};
cnt = (cmiss // c) || (nmiss // n);
print cnt[r=VarName c=Names label=""];
create wes from cnt[rowname=VarName colname=names];
append from cnt[rowname=VarName];
close wes;
quit;

View solution in original post


All Replies
Super User
Posts: 20,236

Re: Send this IML output to dataset instead of result window

Here's a blog post with instructions:

 

https://blogs.sas.com/content/iml/2011/04/18/writing-data-from-a-matrix-to-a-sas-data-set.html

 


ProcWes wrote:

This is either impossible or really easy, but today's the first day I've ever looked at IML.  I am using this code to analyze a dataset to see what percent is missing for each variable.  I need the results in a dataset instead of a result window.

 

proc iml;
use one;
read all var _NUM_ into x[colname=nNames]; 
n = countn(x,"col");
nmiss = countmiss(x,"col");
 
read all var _CHAR_ into x[colname=cNames]; 
close one;
c = countn(x,"col");
cmiss = countmiss(x,"col");
 
/* combine results for num and char into a single table */
Names = cNames || nNames;
rNames = {"    Missing", "Not Missing"};
cnt = (cmiss // c) || (nmiss // n);
print cnt[r=rNames c=Names label=""];
quit;

 

Contributor
Posts: 31

Re: Send this IML output to dataset instead of result window

Thanks for the reply.  I was unable to apply that blog to this code.  Good effort though.

Super User
Posts: 20,236

Re: Send this IML output to dataset instead of result window

Post what you've tried then and we can point out where it's incorrect.

Contributor
Posts: 31

Re: Send this IML output to dataset instead of result window

proc iml;
use one;
read all var _NUM_ into x[colname=nNames]; 
n = countn(x,"col");
nmiss = countmiss(x,"col");
 
read all var _CHAR_ into x[colname=cNames]; 
close one;
c = countn(x,"col");
cmiss = countmiss(x,"col");
 
/* combine results for num and char into a single table */
Names = cNames || nNames;
rNames = {"    Missing", "Not Missing"};
cnt = (cmiss // c) || (nmiss // n);
print cnt[r=rNames c=Names label=""];
create wes;
append from cnt;
close wes;
quit;
Super User
Posts: 20,236

Re: Send this IML output to dataset instead of result window

create wes var {cnt};
append;
close wes;

 This is closer but probably not what you want. I would reshape it using T as indicated in the comments here, where I'm assuming your code also originates from. 

 

https://blogs.sas.com/content/iml/2011/09/19/count-the-number-of-missing-values-for-each-variable.ht...

 

You should also note the comments, PROC FREQ is considered faster this solution is not optimized within IML.

 

 

Contributor
Posts: 31

Re: Send this IML output to dataset instead of result window

Proc Freq can't output all of the variables to a dataset, which is the original issue raised here.
Super User
Posts: 20,236

Re: Send this IML output to dataset instead of result window

Why not? Same out of memory error?


ProcWes wrote:
Proc Freq can't output all of the variables to a dataset, which is the original issue raised here.

 

Contributor
Posts: 31

Re: Send this IML output to dataset instead of result window

No, because Proc Freq doesn't create a table that looks like 

 

Name        Var1    Var2

Missing        4         12

NotMissing   8         0

Super User
Posts: 20,236

Re: Send this IML output to dataset instead of result window

Not by default but it easily can. In this one the format is transposed, from what you indicated, but PROC TRANSPOSE can easily fix that. 

 

https://gist.github.com/statgeek/2de1faf1644dc8160fe721056202f111

Solution
a week ago
Contributor
Posts: 31

Re: Send this IML output to dataset instead of result window

I figured it out on my own.  Posting this for anyone who finds this post later.  This would work to analyze a set to find missing rates of all variables, but it runs out of memory for my large data.

 

proc iml;
use one;
read all var _NUM_ into x[colname=nNames]; 
n = countn(x,"col");
nmiss = countmiss(x,"col");
 
read all var _CHAR_ into x[colname=cNames]; 
close one;
c = countn(x,"col");
cmiss = countmiss(x,"col");
 
/* combine results for num and char into a single table */
Names = cNames || nNames;
VarName = {"Missing", "Not Missing"};
cnt = (cmiss // c) || (nmiss // n);
print cnt[r=VarName c=Names label=""];
create wes from cnt[rowname=VarName colname=names];
append from cnt[rowname=VarName];
close wes;
quit;
SAS Super FREQ
Posts: 3,801

Re: Send this IML output to dataset instead of result window

Contributor
Posts: 31

Re: Send this IML output to dataset instead of result window

How do I mark this as not helpful? This is the same blog Reeza posted earlier.
Super User
Posts: 20,236

Re: Send this IML output to dataset instead of result window


ProcWes wrote:
How do I mark this as not helpful? This is the same blog Reeza posted earlier.

Seriously? Ever heard of the saying, if you don't have something nice to say don't say anything at all.  

 

You realize Rick is the person who originally posted those blogs and takes the time to write them in the first place?

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 191 views
  • 1 like
  • 3 in conversation