Preencher espaço branco

Reply
New Contributor
Posts: 3

Preencher espaço branco

Good afternoon,

 

I'm having a problem, I'm developing a "SQL PROC" and it has a String field, I need to fill in this field with blanks at the end of the field.

Does anyone have a solution for this case?

 

Thank you in advance.

Respected Advisor
Posts: 4,137

Re: Preencher espaço branco

SAS variables of type Character are by "default" filled up with blanks.

 

What's the problem you're having? A newly created character variable not being long enough, or not printing with all the blanks, or....? 

New Contributor
Posts: 3

Re: Preencher espaço branco

So I have a Character variable, I'm creating a " PROC SQL", and a field has a content that in the end needs to fill in empty spaces.

Example: "00001-test" --- Result: "00001-test "

In this example I filled out 10 empty spaces.
Respected Advisor
Posts: 4,137

Re: Preencher espaço branco

a field has a content that in the end needs to fill in empty spaces

 

That's something you don't need to do. A SAS character variable has a fixed length and gets always filled up with blanks at the end up to the defined length of this variable. It then also occupies memory and disk space for the full length - that's why with large character variables where you only save short strings in average option "compress=yes" will save you disk space.

 

If you create a new variable in a data step or a proc sql then use a LENGTH statement to define the variable. If you omit the LENGTH statement then SAS will define the variable implicitely with a length which is equal to the length of the string assigned.

 

proc sql;
  create table test as
  select 
    "00001-test" as myVar_1 length=20,
    "00001-test" as myVar_2
  from sashelp.class(obs=1)
  ;
quit;

proc contents data=test;
run;

 

New Contributor
Posts: 3

Re: Preencher espaço branco

Many thanks for the feedback and help Patrick.

I did the test and still the field does not fill in the blanks at the end, however every time I mouse in the field it says that the field size is equal to 49, but the spaces on the right are not filled.

Note (I took the "compress = yes" option to perform this test).

Thank you.
Respected Advisor
Posts: 4,137

Re: Preencher espaço branco

[ Edited ]

I did the test and still the field does not fill in the blanks at the end, however every time I mouse in the field it says that the field size is equal to 49

 

That's what I'm trying to tell you:

The actual variable is "filled up with blanks" to the end of the length of the variable. That doesn't mean it always prints this way ("print" refers to any way you can look at it).

 

You must actually be aware of this "filled-up" state i.e. when you concatenate variables.

result=myvar1||myvar2; is different from result=trim(myvar1)||myvar2;

 

 

data test;
  length result $20 myVar1 myVar2 $10;
  myVar1='A';
  myvar2='B';
  result=myvar1||myvar2;
  output;
  result=trim(myvar1)||myvar2;
  output;
run;
proc print data=test;
run;

 

 

                       my      my
Obs    result         Var1    Var2

 1     A         B     A       B  
 2     AB              A       B  

 

Ask a Question
Discussion stats
  • 5 replies
  • 136 views
  • 2 likes
  • 2 in conversation