BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Batman
Quartz | Level 8

 

Shouldn't the compress function eliminate the repeating "20"?

27 data check;
28 set in.CK14_data2(firstobs=2 obs=2);
29 put state state hex16.;
30 statex=compress(state,'20'x);
31 put statex statex hex16.;
32 run;

DC 4443202020202020
DC 4443202020202020

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

It did.   Remember that SAS only has two types of variables.  Floating point numbers and fixed length character strings.

 

When you assigned the value 'DC' to the variable the result is that the rest of the variable is filled with spaces.

 

76   data test;
77     length state1 $2 state2 $8 ;
78     state1='DC';
79     state2='DC';
80     put (state1 state2) (=$hex./);
81   run;

state1=4443
state2=4443202020202020

 

View solution in original post

4 REPLIES 4
ballardw
Super User

I think in this case the Put is showing the trailing spaces to the length of the defined variable.

Example:

265  data _null_;
266    length y $ 6;
267    y='D';
268    put  y= hex16. ;
269  run;

y=442020202020

No actual spaces assigned after the D but the result with 5 pairs of 20 in the hex output is shown.

 

This is somewhat analogous to using a 10.8 format to display the value of 1. The format includes the 8 zeroes after the decimal.

SASKiwi
PROC Star

Hexadecimal 20 is the ASCII space character. Character variables in SAS are always padded with spaces beyond the last non-blank character to the length of the variable. That is why compressing space characters will have no effect to the right of the last non-blank character.

ChrisNZ
Tourmaline | Level 20

statex=compress(state,'20'x);

This means: Remove all \x20 characters -aka spaces- from the value stored in the variable STATE.

It's the same as:

statex=compress(state);

and the function does that. Then the value is stored in the variable.

If the variable is longer than the value, the value is right-padded with spaces.

If the variable is shorter than the value, the value is truncated.

 

 

Tom
Super User Tom
Super User

It did.   Remember that SAS only has two types of variables.  Floating point numbers and fixed length character strings.

 

When you assigned the value 'DC' to the variable the result is that the rest of the variable is filled with spaces.

 

76   data test;
77     length state1 $2 state2 $8 ;
78     state1='DC';
79     state2='DC';
80     put (state1 state2) (=$hex./);
81   run;

state1=4443
state2=4443202020202020

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 620 views
  • 2 likes
  • 5 in conversation