DATA Step, Macro, Functions and more

trim function

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

trim function

[ Edited ]

Hello all. i want to use trim function in statement but it gives me error.Bellow is my code. any help to solve the error is appreciated.


data afewwords;
input Word1$ Word2$ ;
cards;
*some/    WHERE
*every*   THING
*no*      BODY
;
select trim(word1) from afewwords ;
proc print ;run;


Accepted Solutions
Solution
‎01-22-2018 01:11 AM
PROC Star
Posts: 2,369

Re: trim function

A lot of confusion this question has.

Like this?

data AFEWWORDS;
input WORD1 $ WORD2 $ ;
cards;
*some/    WHERE
*every*   THING
*no*      BODY
run;
proc sql;
  select trim(WORD1) 'Word1' from AFEWWORDS ;
quit;

 

Word1
*some/
*every*
*no*

View solution in original post


All Replies
Solution
‎01-22-2018 01:11 AM
PROC Star
Posts: 2,369

Re: trim function

A lot of confusion this question has.

Like this?

data AFEWWORDS;
input WORD1 $ WORD2 $ ;
cards;
*some/    WHERE
*every*   THING
*no*      BODY
run;
proc sql;
  select trim(WORD1) 'Word1' from AFEWWORDS ;
quit;

 

Word1
*some/
*every*
*no*
Super User
Posts: 6,781

Re: trim function

A few items to consider ...

 

In a DATA step, CARDS always comes last.  Anything that follows the data is not part of that DATA step.  The programming statements must precede the data.

 

While there is such a thing as a DATA step SELECT statement, it does not function the way you are trying to use it.  What you are doing looks more like SELECT within PROC SQL.

 

If you want to print the first variable, you don't have to change the data.  You can change the report instead:

 

proc print data=afewwords;

var word1;

run;

Super User
Super User
Posts: 8,115

Re: trim function

[ Edited ]

SAS uses fixed length character strings. So the TRIM() function will trim the trailing spaces from the value of a variable, but as soon as you put it back into a variable SAS will pad it with spaces to fill the length of the variable.

 

What do you want to use the TRIM() function to do?  You do not need to use for normal comparisons since SAS knows that 'FRED' and 'FRED    ' are the same thing.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 185 views
  • 3 likes
  • 4 in conversation