BookmarkSubscribeRSS Feed
gtucke1
Fluorite | Level 6

I used proc sort ascending for income categories. One category is still out of place. 

 

Here is my code:

Proc sort data = unmet.needs_final;
by crhouseholdincome;
Run;

Proc freq data = unmet.needs_final;
Table crhouseholdincome;
Run;

8 REPLIES 8
PaigeMiller
Diamond | Level 26

If you are not getting the desired output, SHOW US the output so we can see the problem you are seeing, and explain what you want to see.

--
Paige Miller
Tom
Super User Tom
Super User

Output from WHAT code? On WHAT data?

 

Note that character strings sort differently than the numbers they represent.

Try this code to see a simple example.

data test;
 set sashelp.cars;
 string=left(vvalue(cylinders));
run;
proc freq data=test ;
 tables string cylinders;
run;
The FREQ Procedure

                                   Cumulative    Cumulative
string    Frequency     Percent     Frequency      Percent
-----------------------------------------------------------
.                2        0.47             2         0.47
10               2        0.47             4         0.93
12               3        0.70             7         1.64
3                1        0.23             8         1.87
4              136       31.78           144        33.64
5                7        1.64           151        35.28
6              190       44.39           341        79.67
8               87       20.33           428       100.00


                                      Cumulative    Cumulative
Cylinders    Frequency     Percent     Frequency      Percent
--------------------------------------------------------------
        3           1        0.23             1         0.23
        4         136       31.92           137        32.16
        5           7        1.64           144        33.80
        6         190       44.60           334        78.40
        8          87       20.42           421        98.83
       10           2        0.47           423        99.30
       12           3        0.70           426       100.00

                     Frequency Missing = 2

gtucke1
Fluorite | Level 6

I forgot to include the table.

 

gtucke1_0-1660314322573.png

 

PaigeMiller
Diamond | Level 26

Your variable named crhouseholdincome is a character variable, and these sort alphabetically in SAS. So all of the values that begin with 1 (after the $) will sort together, based upon the characters after the 1. That how alphabetical sorting works, it doesn't know that 100,000 is greater than 11,000. Alphabetic sorting is clearly not what you want.

 

You do want to use a numeric variable, and then these will sort numerically. Instead of creating character variable with values such as '$11,000-$1' (which by the way seems meaningless to me), you want to leave the income as numeric and apply custom formats to the variable. Custom formats change the appearance of the value to be more desirable (as can be seen in the example below) while leaving it as a numeric variable.

 

So let's suppose the variable name is INCOME and it is numeric.

 

proc format;
     value incf 0-10999.99='$0-10,999.99'
        11000-14999.99='$11,000-$14,999.99'
        15000-24999.99='$15,000-$24,999.99' 
         /* I'm lazy, you type the rest */ 
         /* don't forget the semi-colon on the next line */
         ;
run;
proc freq data = unmet.needs_final order=internal;
    table income;
    format income incf.;
run;

 

These will sort numerically.

--
Paige Miller
gtucke1
Fluorite | Level 6

Thank you for your suggestions.

Below is my log noting several errors.

 

iadl_income_3 is the name of the variable from the data set.

 

Value fmtiadl_income_3
ERROR: The format name FMTIADL_INCOME_3 ends in a number, which is invalid.
5860 0-10999.99='$0-10,999.99'
5861 11000-14999.99='$11,000-$14,999.99'
5862 15000-24999.99='$15,000-$24,999.99'
5863 25000-34999.99='$25,000-$34,999'
5864 35000-49999.99='$35,000-$49,999'
5865 50000-74999.99='$50.000-$74,999'
5866 75000-99999.99='$75,000-$99,999'
5867 100,000 + = '$100,000 & over';

ERROR 22-322: Syntax error, expecting one of the following: (, ',', -, <, =.

ERROR 200-322: The symbol is not recognized and will be ignored.

 

I'm not sure how to rectify these errors. 

Tom
Super User Tom
Super User

You cannot end a format name with a digit.  Digits at the end of a format specification are the WIDTH you want to use.  

If you want to make the format name look like the name of ONE of the variables that you might want to use it with and that variable's name ends with a digit then it is common practice to just append the letter F to generate a valid format name.

Tom
Super User Tom
Super User

@gtucke1 wrote:

I used proc sort ascending for income categories. One category is still out of place. 

 

Here is my code:

Proc sort data = unmet.needs_final;
by crhouseholdincome;
Run;

Proc freq data = unmet.needs_final;
Table crhouseholdincome;
Run;


Whether the data is sorted will not have any impact on the output of the PROC FREQ step you showed.  If you want proc FREQ to display the categories in the order they first appear in the data use ORDER=DATA option on the proc FREQ step.

Example:

data test;
 set sashelp.cars;
 string=left(vvalue(cylinders));
run;
proc freq data=test order=data ;
 tables string cylinders;
run;
The FREQ Procedure

                                   Cumulative    Cumulative
string    Frequency     Percent     Frequency      Percent
-----------------------------------------------------------
6              190       44.39           190        44.39
4              136       31.78           326        76.17
8               87       20.33           413        96.50
10               2        0.47           415        96.96
3                1        0.23           416        97.20
.                2        0.47           418        97.66
12               3        0.70           421        98.36
5                7        1.64           428       100.00


                                      Cumulative    Cumulative
Cylinders    Frequency     Percent     Frequency      Percent
--------------------------------------------------------------
        6         190       44.60           190        44.60
        4         136       31.92           326        76.53
        8          87       20.42           413        96.95
       10           2        0.47           415        97.42
        3           1        0.23           416        97.65
       12           3        0.70           419        98.36
        5           7        1.64           426       100.00

                     Frequency Missing = 2

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1145 views
  • 2 likes
  • 3 in conversation