DATA Step, Macro, Functions and more

how to remove space from the first charcter in the variables

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 115
Accepted Solution

how to remove space from the first charcter in the variables

i have a variable in the dataset and it has characters which start with a weird space or square like thing.

I need to remove it whereever it is there in the variable list. Can any one help me in it.


Accepted Solutions
Solution
‎09-02-2016 04:36 AM
Super User
Posts: 9,687

Re: how to remove space from the first charcter in the variables

temp1=prxchange('s/^\s+//',1,temp);

View solution in original post


All Replies
Super User
Posts: 6,963

Re: how to remove space from the first charcter in the variables

First I'd recommend to convert the contents of your variable to a new variable of double length with a $hex format.

Assuming that oldvar is $6:

newvar = put(oldvar,$hex12.);

Now you can determine what exactly is behind the funny symbol, and can use that hex code to decide when the first character should be stripped using the substr() function and when not. Use the hex code in a literal as "NN"x (NN being hex digits 0-9,a-f)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 115

Re: how to remove space from the first charcter in the variables

Yes, But not all variables have that funny symbol in the start.

For example see the screenshot. but for some variables it is proper.

 


Capture.JPG
Super User
Posts: 6,963

Re: how to remove space from the first charcter in the variables

Since this is program code, it looks to me like the result of a failed character code conversion when transferring the file from one system to another.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 1,400

Re: how to remove space from the first charcter in the variables

The rectangle means it is a non printable character and is not in your font set of characters.

I would start with searching the origin of data because removing characters means you change the data.

 

On your capture I saw 0 to 2 rectangles and maybe there are more, even in middle of a variable; 

Those can be different characters, not neccessary the same one;

 

in case you recognize specific hexa characters you can replace then to any character(s) you chose

using TRANSLATE function:

      ne_var = translate(old_var,  new_chars, old_chars);

new_chars and old_chars should fit by order.

 

 

 

 

 

Trusted Advisor
Posts: 1,400

Re: how to remove space from the first charcter in the variables

you can remove 1st character by:

var = substr(var,2);

 

Do you know the origin of your data? how did you get the variable with that extra character ?

Solution
‎09-02-2016 04:36 AM
Super User
Posts: 9,687

Re: how to remove space from the first charcter in the variables

temp1=prxchange('s/^\s+//',1,temp);
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 272 views
  • 1 like
  • 4 in conversation