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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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