BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ProcWes
Quartz | Level 8

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;
1 ACCEPTED SOLUTION

Accepted Solutions
ProcWes
Quartz | Level 8

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

13 REPLIES 13
Reeza
Super User

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;

 

ProcWes
Quartz | Level 8

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

Reeza
Super User

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

ProcWes
Quartz | Level 8
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;
Reeza
Super User
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.

 

 

ProcWes
Quartz | Level 8
Proc Freq can't output all of the variables to a dataset, which is the original issue raised here.
Reeza
Super User

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.

 

ProcWes
Quartz | Level 8

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

 

Name        Var1    Var2

Missing        4         12

NotMissing   8         0

Reeza
Super User

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

ProcWes
Quartz | Level 8

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;
ProcWes
Quartz | Level 8
How do I mark this as not helpful? This is the same blog Reeza posted earlier.
Reeza
Super User

@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?

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 13 replies
  • 7836 views
  • 1 like
  • 3 in conversation