Proc Format variable order

New Contributor
Posts: 2

Proc Format variable order


I am trying to have my format to read exactly as below but in order for it to work I had to put in ZZ to force it to keep last

Here is my code

Proc Format;

Value $test;

"NIA Lvl 1"= "NIA 1"

"NIA Lvl 2"="NIA  2"

"NIA Lvl 3"="NIA 3"


"NIA O"= NIA Old"

"NIA No"=NIA None"

NIA Miss"=NIA Missing"


Data Qtr2;

set Qtr;


do count =1 to 15;

if try {count}=. then try {count}=0;


If NIA= NIA No" then "ZNIA None";

if NIA="NIA Miss" then "ZZ NIA Missing";


I then create a PDF report  However, the zz show up in my report and it is in the order listed.  how can i keep the order just as listed but without the ZZ showing on the report

Super User
Posts: 17,819

Re: Proc Format variable order

You're missing quotation marks above but I'll assume that's a result of moving code from your program editor to here and not the issue. You can double check your log to see if it's an issue.

You show a format but don't show it being used so its hard to say what you're doing.

To get the order you want in SAS I highly recommend coding variables to numeric values and then applying the format you want to the data.


Proc Format;

Value $test;

"NIA Lvl 1"= 1

"NIA Lvl 2"=2

"NIA Lvl 3"=3

"NIA X" =4

"NIA O"= 5

"NIA No"=6

"NIA Miss"=7;

value test_display

1 = "NIA 1"

2 = "NIA 2"

3 = "NIA 3"

4 = "NIA XYZ"

5 = "NIA Old"

6 = "NIA None"

7 = "NIA Missing";


data qtr2;

set qtr;

*your other code;

nia_temp=put(nia, $test);

format nia_test test_display.;


New Contributor
Posts: 2

Re: Proc Format variable order

Actually I did not paste over the entire code and I did not have any missing or mismatched errors.  I was trying to minimize the code due to confidentiality.  I will just rewrite dummy code so it makes more sense to you all. 

Posts: 8,743

Re: Proc Format variable order


  I notice a few things:

1) I am surprised your code works, for example, you have quite a few mismatched or missing quotes and at least one missing semicolon (on your ARRAY) statement.

2) I would have expected some kind of error because you never show TRY as the name of the array in your ARRAY statement, nor do you list or show the variables that are array elements. This is the type of error you get for an undeclared array:

ERROR: Undeclared array referenced: try.

ERROR: Variable try has not been declared as an array.


3) You do not show where you are using the $TEST format in your code. However, that piece of PROC FORMAT code also has mismatched quotes, so I doubt whether you are defining the format correctly, if at all.

4) You say that after this DATA step (which may have problems), you then create a PDF report -- what code do you use for the report? Is that where you are using the $TEST format? Are you using a PROC SORT step? PROC MEANS? PROC PRINT? PROC REPORT? PROC TABULATE? PROC ???

5) Without seeing your data, it's a bit hard to guess how the NIA variables are being used -- does each obs have multiple NIA variables? does each obs have 1 NIA variable? How are you using the NIA variables to get your report? Are you, for example, using them as CLASS variables in PROC MEANS? Using them at VAR variables in PROC TABULATE? Using them as GROUP or ORDER variables in PROC REPORT? How you get a particular order will depend on the procedure you're using and how you're using $TEST.

It would help the other forum participants to help you if you:

--showed a bit of your data, or tried to replicate your issue using some fake data

--showed ALL your code, including ODS statements and the procedure code where you use $TEST

--showed WORKING code (code that someone could cut and paste without getting errors on mismatched quotes, missing ARRAY names and missing semi-colons


Occasional Contributor
Posts: 10

Re: Proc Format variable order

I think Reeza's reply is pretty much your  best bet. The original values sort in a different order than what you want, and unless you want to change the formatted values, you need to restructure the data.

One note on Reeza's post: the PUT statement creates a character variable, so the test_display format should be $test_display.

Another way to go would be to create a format like your original one, but with formatted values that follow the preferred order, eg:

Value $test

"NIA Lvl 1"= "a. NIA 1"

"NIA Lvl 2"="b. NIA  2"

"NIA Lvl 3"="c. NIA 3"

"NIA X"= "d. NIA XYZ"

"NIA O"= "e. NIA Old"

"NIA No"="f. NIA None"

"NIA Miss"="g. NIA Missing"


and then use the ORDER=FORMATTED option on the PROC FREQ (also works on PROC TABULATE, I think). However, for PROC SUMMARY and similar activities, the original values determine the sort order in the output data.

Posts: 7,363

Re: Proc Format variable order

The responses you have gotten all appear to be related to the pseudo code you provided, but I don't think they addressed your question.  If you simply want to exclude records that have the undesired value "NIA Miss", couldn't you just exclude them using a where option (e.g.,

(where=(nia ne "NIA Miss")) when you declare your data in the proc that is producing your report?

Super User
Posts: 17,819

Re: Proc Format variable order

I assumed the OP wanted the results but without the ZZ added in that pushed them to the end.

Respected Advisor
Posts: 3,777

Re: Proc Format variable order

Sounds like you want the PROC FORMAT option NOTSORTED combined with with ORDER=DATA.  Consider the following code.

Proc Format;

   Value $nia(notsorted)

      "NIA Lvl 1"= "NIA 1"

      "NIA Lvl 2"="NIA  2"

      "NIA Lvl 3"="NIA 3"

      "NIA X" = "NIA XYZ"

      "NIA O"= "NIA Old"

      "NIA No"="NIA None"

      "NIA Miss"="NIA Missing"



data test;

   length nia $16;

   nia = "NIA Lvl 1";



proc summary data=test nway completetypes;

   class nia / preloadfmt order=data;

   format nia $nia.;

   output out=example / levels;


proc print;


Ask a Question
Discussion stats
  • 7 replies
  • 6 in conversation