Desktop productivity for business analysts and programmers

Set character variable to 3 blank spaces

Reply
New Contributor
Posts: 2

Set character variable to 3 blank spaces

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;

Super User
Posts: 20,252

Re: Set character variable to 3 blank spaces


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;

 

 

New Contributor
Posts: 2

Re: Set character variable to 3 blank spaces

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.  

Super User
Posts: 5,620

Re: Set character variable to 3 blank spaces

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;

Ask a Question
Discussion stats
  • 3 replies
  • 109 views
  • 0 likes
  • 3 in conversation