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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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