BookmarkSubscribeRSS Feed
Shwam
Calcite | Level 5

Hi

 

I am having trouble selecting blank values as a space.

 

We have a requirement where we need to provide a file to the business.

Blank values must = ' ' (a space).

 

I am attempting to use the below case statement (which works on MS sql and oracle - I am new to SAS).

 

case when INTERRUPTION_TYPE = '' then ' '
else INTERRUPTION_TYPE end as INTERRUPTION_TYPE

 

The results return the blank values as a blank - no space.

 

When I use the below, it will return an x so I know that my query is picking up the correct values but not returning a space when i want it to.

case when INTERRUPTION_TYPE = '' then 'x' 
else INTERRUPTION_TYPE end as INTERRUPTION_TYPE

 

Please help!

 

Thanks in advance!

 

 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

How are you sending the file to the "business"?   The reason being is that " " is just an empty string in SAS, you can put out data which "looks" like spaces, this for instance puts spaces to the length of the string.

data want;
  set sashelp.class;
  if sex="M" then res=" ";
  else res="X";
  put "Res='" res "'";
run;

So the question is how you are writing data to this "file" you mention.

Kurt_Bremser
Super User

Keep in mind that SAS character variables are always padded with blanks. Try this:

data test;
length x1 $10 x2 $20;
x1 = '';
x2 = put(x1,$hex20.);
run;

and look at the resulting dataset.

Shwam
Calcite | Level 5

Thanks for the quick response.

 

Will play around with this as soon as I'm back in the office tomorrow morning.

 

There any chance you could help with a solution in proc sql?

My data step skills are very entry level and we have written the entire script in sql

Kurt_Bremser
Super User

My data step is only there to illustrate that

'' = ' '

in SAS. Since SAS does not know a varchar type, but only character with a given length (>= 1), all empty/missing character variables will at least contain one blank, so a statement in SQL like

case
  when X = ''
  then ' '
  else X
end as X

won't change anything. You need to redefine your logic in light of this fact.

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