BookmarkSubscribeRSS Feed
drocumback
Fluorite | Level 6

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.

5 REPLIES 5
Patrick
Opal | Level 21

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....? 

drocumback
Fluorite | Level 6
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.
Patrick
Opal | Level 21

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;

 

drocumback
Fluorite | Level 6
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.
Patrick
Opal | Level 21

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  

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 922 views
  • 2 likes
  • 2 in conversation