SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fengyuwuzu
Pyrite | Level 9

I have special characters in my data,  ≥ and ≤. without any format, I can produce the freq table as below:

 

undefined

 

But I want to order the age groups, so they are in order, and tried to use the following code to create a format. However, the format does not work.

 

I found a old post in 2013 mentioning running SAS in unicode mode, but I do not know how to run SAS in unicode mode. Can nay one help? Thank you.

 

PROC FORMAT;
   VALUE $ typefmt 
        1 = '(*ESC*){unicode '"2264"x} 20'
        2 = '21-25 '
        3 = '26-30 ' 
	4 = '(*ESC*){unicode "2265"x} 31'
;
quit;
proc freq data=new_vvnv;
tables age_group*status /nocol  ;
format age_group $typefmt.;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

I think, you could simply sort

 

by age;

rather than by age_group age to achieve the desired result.

 

 

Still, it would be interesting to find out why sorting by AGE_GROUP, which should contain values '1', '2', '3' and '4', fails to bring '1' to the top. (PROC SORT always sorts by unformatted values.) My next step would be a closer look at the unformatted values of AGE_GROUP.

 

View solution in original post

8 REPLIES 8
LinusH
Tourmaline | Level 20
I'm not following.
The freq output looks like the code you are supplying?
And from this code it seems that your data contains 1, 2, 3 and 4 - not any special characters...?
Data never sleeps
Cynthia_sas
SAS Super FREQ

Hi: Since you did not provide data to test with I made some fake data from SASHELP.CLASS. But using ORDER=DATA (after I'd sorted the file) worked for me to put the AGE_GROUP in order.

 

cynthiaundefined

fengyuwuzu
Pyrite | Level 9

I got the following error:

 

42   PROC FORMAT;
43      VALUE $ typefmt
44           1 = '(*ESC*){unicode '"2264"x} 20 '
                                               --------------
                                               49
45           2 = '21-25 '
                        --------------
                        49
46           3 = '26-30 '
                    ---
                    22
                    76
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release.  Inserting white
             space between a quoted string and the succeeding identifier is recommended.

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

ERROR 76-322: Syntax error, statement will be ignored.

47       4 = '(*ESC*){unicode "2265"x} 31 '
48   ;
49   quit;
50   proc freq data=new_vvnv;
51   tables age_group*status /nocol  ;
52   format age_group $typefmt.;
53   run;

FreelanceReinh
Jade | Level 19

Please remove the single quote in

... unicode '"2264"x ...
fengyuwuzu
Pyrite | Level 9

Here is the data

 

Obs Age Age_Group 
1 21 21-25 
2 21 21-25 
3 21 21-25 
4 21 21-25 
5 21 21-25 
6 21 21-25 
7 21 21-25 
8 21 21-25 
9 21 21-25 
10 21 21-25 
11 21 21-25 
12 21 21-25 
13 22 21-25 
14 22 21-25 
15 22 21-25 
16 22 21-25 
17 22 21-25 
18 23 21-25 
19 23 21-25 
20 23 21-25 
21 23 21-25 
22 23 21-25 
23 23 21-25 
24 24 21-25 
25 24 21-25 
26 24 21-25 
27 24 21-25 
28 24 21-25 
29 24 21-25 
30 25 21-25 
31 25 21-25 
32 25 21-25 
33 25 21-25 
34 25 21-25 
35 26 26-30 
36 27 26-30 
37 27 26-30 
38 28 26-30 
39 28 26-30 
40 1820 
41 1820 
42 1820 
43 1820 
44 1820 
45 1820 
46 1820 
47 1820 
48 1820 
49 1820 
50 1920 
51 1920 
52 1920 
53 1920 
54 1920 
55 1920 
56 1920 
57 1920 
58 1920 
59 1920 
60 1920 
61 1920 
62 1920 
63 1920 
64 2020 
65 2020 
66 2020 
67 2020 
68 2020 
69 2020 
70 2020 
71 2020 
72 2020 
73 2020 
74 2020 
75 2020 
76 3131 
77 3531 

 

I used the following code, now format is okay (thanks for pointing out a single quote in original code), but the sort does not work. After sort, 21-25 is still the first age group.

 

PROC FORMAT;
   VALUE $ typefmt 
        1 = '(*ESC*){unicode "2264"x} 20 '
        2 = '21-25 '
        3 = '26-30 ' 
	4 = '(*ESC*){unicode "2265"x} 31 '
;
quit;

proc sort data=new_vvnv;
format age_group $typefmt.;
by age_group age;
run;


proc freq data=new_vvnv order=data;
tables age_group*status /nocol  ;
format age_group $typefmt.;
run;

 

 

 

FreelanceReinh
Jade | Level 19

I think, you could simply sort

 

by age;

rather than by age_group age to achieve the desired result.

 

 

Still, it would be interesting to find out why sorting by AGE_GROUP, which should contain values '1', '2', '3' and '4', fails to bring '1' to the top. (PROC SORT always sorts by unformatted values.) My next step would be a closer look at the unformatted values of AGE_GROUP.

 

fengyuwuzu
Pyrite | Level 9

yes simply sort by age works. Thanks

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 8 replies
  • 17251 views
  • 1 like
  • 4 in conversation