BookmarkSubscribeRSS Feed
Calcite | Level 5


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

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.;


Calcite | Level 5

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. 



  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


Fluorite | Level 6

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.

Opal | Level 21

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

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

Jade | Level 19

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;



Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.


Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 6 in conversation