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

Hello,

 

I have just found the nead flow="tables" option to ods excel which prevents the insertion of newlines making tables much easier to reformat. Unfortunately, there is also a side effect: one can no longer custom sort the values by inserting spaces into the format such as this:

proc format;
    value $custsort
        "2" = "  2"
        "1" = " 1"
        "3" = "3";

This is because enabling the flow option disables the automatic stripping of the values resulting in the sorting spaces being visible in the final output. Is there a way of preventing the newline insertion and keeping the values stripped at the same time? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

With proc report you are golden. Consider:

 

proc format;
	value $custsort (notsorted)
		"USA" = "  USA"
		"Asia" = " Asia"
		"Europe" = "Europe";
quit;



proc report data=sashelp.cars;
	columns Origin Type, Horsepower;
	define Origin / " " group preloadfmt order=data format=$custsort.;
	define Type / " " across;
	define Horsepower / " " analysis max;
run;

You can use the same approach for any format and shouldn't need the inserted spaces you had used. Do note that the format definition needs the Notsorted option and there are restrictions to use Preloadfmt thought the order=data is likeliest. This will also provide a row for the output for the format even if no values are encountered.

View solution in original post

6 REPLIES 6
ballardw
Super User

How are you creating the tables that you are displaying?

 

A few procedures, such as Proc Tabulate and Report, allow use of PRELOADFMT that will maintain an order if the format is defined with the Notsorted option and the correct options are chosen.

js5
Pyrite | Level 9 js5
Pyrite | Level 9

The tables are created with proc report. I am then using the format similar to the one displayed above for the group variables:

 

ETA: added code example

proc format;
	value $custsort
		"USA" = "  USA"
		"Asia" = " Asia"
		"Europe" = "Europe";
quit;

ods excel file="C:\Users\&sysuserid\test.xlsx" options(flow="tables");

proc report data=sashelp.cars;
	columns Origin Type, Horsepower;
	define Origin / " " group format=$custsort.;
	define Type / " " across;
	define Horsepower / " " analysis max;
run;

ods excel close;
ballardw
Super User

With proc report you are golden. Consider:

 

proc format;
	value $custsort (notsorted)
		"USA" = "  USA"
		"Asia" = " Asia"
		"Europe" = "Europe";
quit;



proc report data=sashelp.cars;
	columns Origin Type, Horsepower;
	define Origin / " " group preloadfmt order=data format=$custsort.;
	define Type / " " across;
	define Horsepower / " " analysis max;
run;

You can use the same approach for any format and shouldn't need the inserted spaces you had used. Do note that the format definition needs the Notsorted option and there are restrictions to use Preloadfmt thought the order=data is likeliest. This will also provide a row for the output for the format even if no values are encountered.

js5
Pyrite | Level 9 js5
Pyrite | Level 9

Thanks @ballardw, your suggestion works! I am not getting rows for empty categories though with this code:

proc format;
	value $custsort (notsorted)
		"Dummy" = "Dummy"
		"USA" = "USA"
		"Asia" = "Asia"
		"Europe" = "Europe";
quit;

ods excel file="C:\Users\&sysuserid\test.xlsx" options(flow="tables");

proc report data=sashelp.cars;
	columns Origin Type, Horsepower;
	define Origin / " " group preloadfmt order=data format=$custsort.;
	define Type / " " across;
	define Horsepower / " " analysis max;
run;

ods excel close;
Ksharp
Super User

Or try other blank character like TAB character ?

 

proc format;
	value $custsort
		"USA" = "'09'x '09x' USA"
		"Asia" = "'09'x  Asia"
		"Europe" = "Europe";
quit;
ballardw
Super User

@Ksharp wrote:

Or try other blank character like TAB character ?

 

proc format;
	value $custsort
		"USA" = "'09'x '09x' USA"
		"Asia" = "'09'x  Asia"
		"Europe" = "Europe";
quit;

And I though I was bad using ASCII null characters, hold down ALT key and enter 255 on the numeric pad in Windows, to force indents before style ASIS was available. One headache I ran into with this approach was remembering that I had typed nulls and not spaces so extra comments were a very good idea.

 

The OP wanted the order but not the appearance of leading spaces and I can see the results of tabs as spaces in Excel.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 4124 views
  • 1 like
  • 3 in conversation