BookmarkSubscribeRSS Feed
alamas
Calcite | Level 5

I am working in SAS EG and am trying to edit a dataset so that if a character variable is not equal to 'XYZ' then it should be set to '   ' (which is 3 blank spaces).  However when I do this, it is actually set to missing which is ''.  Is there a way to set an observation to be a certain amount of blank spaces?   

 

Example code:

data new;

set old;

if AccountCode ^= 'XYZ' then AccountCode = '   ';

run;

3 REPLIES 3
Reeza
Super User

@alamas wrote:

I am working in SAS EG and am trying to edit a dataset so that if a character variable is not equal to 'XYZ' then it should be set to '   ' (which is 3 blank spaces).  However when I do this, it is actually set to missing which is ''.  Is there a way to set an observation to be a certain amount of blank spaces?   

 

Example code:

data new;

set old;

if AccountCode ^= 'XYZ' then AccountCode = '   ';

run;


Why are you trying to do this? There may be other options that work if we know what you're trying to do. 

 

Technically if SAS is missing it still has the length of the variable, so it's a length of whatever the variable is, see the LENGTH, LENGTHN/LENGTHC functions.  

 

data want;
    length name $15.;
    set sashelp.class;

    if name in ('Alfred', 'Jane') then
        name='   ';
    x1=length(name);
    x2=lengthc(name);
    x3=lengthn(name);
    keep name x1-x3;
run;

 

 

alamas
Calcite | Level 5

Thanks for the suggestion.  I tried your code but when I set the name = '   ' then the length(name) of that observation is 1 and the lengthn(name) is 0 rather than 3 for the blank spaces.  

Astounding
PROC Star

IF THEN statements do not have the ability to change the length of a character variable.  If you are seeing fewer than three blanks, that's because of the tools you are using to view the data.  If AccountCode had a length of three originally, it still has a length of 3. 

 

To confirm this, you could try (within a DATA step of course):

 

test_string = '*' || AccountCode || '*';

 

put test_string;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 3 replies
  • 1406 views
  • 0 likes
  • 3 in conversation