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;
@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;
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: