data test;
dr1='dxxxxxxxx';
dr2=' bxxxxxxxx';
dr3=' daxxxxxxxxx';
dr4=' vxxxxxxxx';
run;
data test1;;
set test;
length Drx1-Drx4 $100;
Drx1=strip(dr1);
Drx2=strip(dr2);
Drx3=strip(dr3);
Drx4=strip(dr4);
call sortc (of drx1-drx4);
run;
Expected order is:
bxxxxxxxx daxxxxxxxxx dxxxxxxxx vxxxxxxxx
but the output is not expected output
Are you sure? Your example worked perfectly for me. What version of SAS are you using?
@SASPhile wrote:
data test;
dr1='dxxxxxxxx';
dr2=' bxxxxxxxx';
dr3=' daxxxxxxxxx';
dr4=' vxxxxxxxx';
run;
data test1;;
set test;
length Drx1-Drx4 $100;
Drx1=strip(dr1);
Drx2=strip(dr2);
Drx3=strip(dr3);
Drx4=strip(dr4);
call sortc (of drx1-drx4);
run;
Expected order is:
bxxxxxxxx daxxxxxxxxx dxxxxxxxx vxxxxxxxx
but the output is not expected output
Does your log show any errors such that a previous data set wasn't replaced? Or are you discussing other data where the XXX values are different than actually shown? You may have hidden something that affects the sort order with the xs.
Show your actual result. Mine matches your expected as well.
No Errors in log:
1 data test;
2 dr1='dxxxxxxxx';
3 dr2=' bxxxxxxxx';
4 dr3=' daxxxxxxxxx';
5 dr4=' vxxxxxxxx';
6 run;
NOTE: The data set WORK.TEST has 1 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.14 seconds
cpu time 0.03 seconds
7
8
9
10 data test1;;
11 set test;
12 length Drx1-Drx4 $100;
13
14 Drx1=strip(dr1);
15 Drx2=strip(dr2);
16 Drx3=strip(dr3);
17 Drx4=strip(dr4);
18 call sortc (of drx1-drx4);
19 run;
NOTE: There were 1 observations read from the data set WORK.TEST.
NOTE: The data set WORK.TEST1 has 1 observations and 8 variables.
NOTE: DATA statement used (Total process time):
real time 0.08 seconds
cpu time 0.03 seconds
Screen or print of output?
My debugging steps would be:
1. Restart SAS
2. I would check your SAS options, see if any are related to sorting,
3. and then if it persists, contact SAS tech support.
Works fine for me.
233 data test1;; 234 set test; 235 length Drx1-Drx4 $20; 236 237 Drx1=strip(dr1); 238 Drx2=strip(dr2); 239 Drx3=strip(dr3); 240 Drx4=strip(dr4); 241 call sortc (of drx1-drx4); 242 243 put (_all_) (=:$quote. /) ; 244 run; dr1="dxxxxxxxx" dr2=" bxxxxxxxx" dr3=" daxxxxxxxxx" dr4=" vxxxxxxxx" Drx1="bxxxxxxxx" Drx2="daxxxxxxxxx" Drx3="dxxxxxxxx" Drx4="vxxxxxxxx" NOTE: There were 1 observations read from the data set WORK.TEST. NOTE: The data set WORK.TEST1 has 1 observations and 8 variables.
Perhaps those are not actual spaces in the beginning of your variable values?
246 data test; 247 dr1='dxxxxxxxx'; 248 dr2=' bxxxxxxxx'; 249 dr3=' daxxxxxxxxx'; 250 dr3='00'x || 'daxxxxxxxxx' ; 251 dr4=' vxxxxxxxx'; 252 run; NOTE: The data set WORK.TEST has 1 observations and 4 variables. 253 254 data test1;; 255 set test; 256 length Drx1-Drx4 $20; 257 258 Drx1=strip(dr1); 259 Drx2=strip(dr2); 260 Drx3=strip(dr3); 261 Drx4=strip(dr4); 262 call sortc (of drx1-drx4); 263 264 put (_all_) (=:$quote. /) ; 265 run; dr1="dxxxxxxxx" dr2=" bxxxxxxxx" dr3=" daxxxxxxxxx" dr4=" vxxxxxxxx" Drx1=" daxxxxxxxxx" Drx2="bxxxxxxxx" Drx3="dxxxxxxxx" Drx4="vxxxxxxxx" NOTE: There were 1 observations read from the data set WORK.TEST. NOTE: The data set WORK.TEST1 has 1 observations and 8 variables.
how to remove the leading spaces ? I tried compress,strip etc. Nothing seems to do the trick.
It is NOT a space. That is why STRIP() didn't work.
So ask SAS to tell you what character it is.
put dr3 $hex2. ;
Then tell COMPRESS() to remove that character. So if it is a binary zero like in my example you could use this:
drx3=strip(compress(dr3,'00'x));
The values I'm getting after using
put dr3 $hex2. ;
65
C2
C2
C2
C2
C2
C2
@SASPhile wrote:
The values I'm getting after using
put dr3 $hex2. ;
65
C2
C2
C2
C2
C2
C2
'65'x is just a lower case E. But since 'C2'x has the high order bit set it is NOT a normal ASCII code. So its meaning depends on the encoding setting you are using. If you where using LATIN encoding it would probably be an uppercase A with umlaut over it. Since you say it looks like a space you are probably using UTF8 encoding so the meaning depends on the characters that follow it. Run the simple test again with a longer width on the $HEX. format to see the hex codes for more bytes in the string.
Most likely you have the string 'C2A0'x which is UTF8 code for a "non-breaking" space. COMPRESS() might not be the right tool to use to remove that since it works on single bytes. So if you asked compress to remove 'C2A0'x it would remove ALL of the bytes that were either 'C2'x or 'A0'x. So if you had some other UTF8 code that started with 'C2' it get messed up. So try using TRANWRD() instead.
drx3 = strip(tranwrd(dr3,'C2A0'x,' '));
You should also look into HOW this non-ASCII code got into your variable's value to begin with.
I used compress(dr3,,'kw') and it worked. (keep only printable characters)! Thanks again for your help!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.