BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8


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

11 REPLIES 11
Reeza
Super User

Are you sure? Your example worked perfectly for me. What version of SAS are you using?

 

delete_drx.PNG


@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




ballardw
Super User

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.

SASPhile
Quartz | Level 8

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

Reeza
Super User

 

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. 

 

Reeza
Super User
Check the SORTSEQ option.
Tom
Super User Tom
Super User

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.
SASPhile
Quartz | Level 8

how to remove the leading spaces ? I tried compress,strip etc. Nothing seems to do the trick.

Tom
Super User Tom
Super User

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));
SASPhile
Quartz | Level 8

The values I'm getting after using

put dr3 $hex2. ;

 

65
C2
C2
C2
C2
C2
C2

Tom
Super User Tom
Super User

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

SASPhile
Quartz | Level 8

I used compress(dr3,,'kw') and it worked. (keep only printable characters)! Thanks again for your help!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 11 replies
  • 1288 views
  • 0 likes
  • 4 in conversation