BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
FP12
Obsidian | Level 7

Hi all,

 

I dcan't manage to use trim function in a proc sql, don't understand why.

I have a table A with a variable B which format is character(6):

I do this:

%global b;

PROC SQL NOPRINT;
	SELECT TRIM(B) INTO :b
	FROM A
	WHERE ROW_NUMBER = 1;
QUIT;
%put ___&b.___;

The result in the log is:

___A1    ___

I surely miss something, can you help me finding it?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

When creating macro variables you need the TRIMMED option.

 

%global b;

PROC SQL NOPRINT;
	SELECT B INTO :b TRIMMED
	FROM A
	WHERE ROW_NUMBER = 1;
QUIT;
%put ___&b.___;

View solution in original post

4 REPLIES 4
Reeza
Super User

When creating macro variables you need the TRIMMED option.

 

%global b;

PROC SQL NOPRINT;
	SELECT B INTO :b TRIMMED
	FROM A
	WHERE ROW_NUMBER = 1;
QUIT;
%put ___&b.___;
FP12
Obsidian | Level 7

Many thanks for this.

Is it only because I declared the macrovariable before entering the PROC SQL?

It is strange this two ways of writing...

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Really, as:

proc sql noprint;  
  select trim(" abc    ") into :r from sashelp.class where name="Alfred";
quit;
%put ___&r.___;

Works fine.  You may also be better off using strip() function rather than trim()

Tom
Super User Tom
Super User

SAS stores character variables as fixed length variables. Since you didn't tell SQL what length to make the new variable you created with the TRIM() function call its best guess was to make it the same length as the variable going into the TRIM() function. So then when you pushed the value into a macro variable it included the spaces needed to pad the trimmed value back out to the full length of the variable.

 

You can use the TRIMMED keyword as part of the INTO clause to remove the trailing spaces.

proc sql noprint;
select b 
  into :b trimmed
  from a
  where row_number = 1
;
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 17376 views
  • 1 like
  • 4 in conversation