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

I'm running this code.

* show dataset info;
proc iml;
use sashelp.class;
show contents;
quit;

* show dataset missing;
proc iml;
use sashelp.class;
read all var _NUM_ into x[colname=nNames];
n = countn(x,"col");
nmiss = countmiss(x,"col");
ntotal = n + nmiss;
n_p = (n / ntotal) * 100;
nmiss_p = (nmiss / ntotal) * 100;

read all var _CHAR_ into x[colname=cNames];
c = countn(x,"col");
cmiss = countmiss(x,"col");
ctotal = c + cmiss;
c_p = (c / ctotal) * 100;
cmiss_p = (cmiss / ctotal) * 100;
close data.stl_2p;

Names = cNames || nNames;
colNames = {"Missing", "Not Missing"};
cnt = (cmiss_p // c_p) || (nmiss_p // n_p);
cnt = cnt`;
print cnt[r=Names c=colNames label="Percent(%)" format=5.2];
quit;

The output for "show contents" can be pasted nicely into comments, but the "print ..." table cannot.

Is there a way to format the results of the print statement to be similar to "show contents"?

 

I just want the table values to be aligned with the columns when I copy paste it into the comments.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Sure. you are probably copying from the HTML destination. You can preserve spacing by copying from the LISTING destination, which uses a monospace (fixed-width) font.

 

ods listing;
print cnt[r=Names c=colNames label="Percent(%)" format=5.2];
ods listing close;

 

You can now copy/paste the results from the Output window. If you can't find the Output window, tell us how you are running the code: SAS Studio? Enterprise Guide? Classic Windowing system (DMS)?

View solution in original post

7 REPLIES 7
Reeza
Super User
Can I ask why you're using IML here instead of PROC FREQ or TABULATE with a more controlled output format?
publicSynechism
Fluorite | Level 6

I normally use

proc format;
	value $missfmt ' '='Missing' other='Not Missing';
	value  missfmt  . ='Missing' other='Not Missing';
run;

proc freq data=sashelp.class;
format _NUMERIC_ missfmt.;
table _NUMERIC_ / missing;
format _CHAR_ $missfmt.;
table _CHAR_ / missing;
run;

But I wanted to keep all values in a single table.

 

If there is an easier method that does exactly this, then I'd like to use that.

 

However, my main problem is just getting a nicely aligned table that can be copy pasted back into the editor without first writing to another file.

Reeza
Super User

I have a version here if you’d like that collects all the info cleanly. 

 

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

 

 

publicSynechism
Fluorite | Level 6

@ReezaThank you for sharing this.

Ksharp
Super User

Why not post it at IML forum ? @Rick_SAS  is there .

Rick_SAS
SAS Super FREQ

Sure. you are probably copying from the HTML destination. You can preserve spacing by copying from the LISTING destination, which uses a monospace (fixed-width) font.

 

ods listing;
print cnt[r=Names c=colNames label="Percent(%)" format=5.2];
ods listing close;

 

You can now copy/paste the results from the Output window. If you can't find the Output window, tell us how you are running the code: SAS Studio? Enterprise Guide? Classic Windowing system (DMS)?

publicSynechism
Fluorite | Level 6

@Rick_SASThis output exactly what I was looking for. Thank you.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 7 replies
  • 2407 views
  • 2 likes
  • 4 in conversation