turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Trim and left together

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-05-2015 04:51 AM

Hi all,

Lets x="_ _ab_cd_ _";

(Assuming _ as a blank )

y=trim(left(x));

z=left(trim(x));

Ideally y and z should not be same.

But while showing outputs of y and z some times its showing the expected (y having no leading and trailing blanks and z having two trailing blanks) and some times not .

What is the cause?

Please suggest.

Regards,

Devi

Lets x="_ _ab_cd_ _";

(Assuming _ as a blank )

y=trim(left(x));

z=left(trim(x));

Ideally y and z should not be same.

But while showing outputs of y and z some times its showing the expected (y having no leading and trailing blanks and z having two trailing blanks) and some times not .

What is the cause?

Please suggest.

Regards,

Devi

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-05-2015 05:31 AM

Hi,

Please clarify what is not expected, provide some example strings in the form of a datastep and where they do not appear as you expect. The help on each of these functions is quite clear:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212224.htm

https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212226.htm

And I do not see why you would use the two functions together, unless in concatentaion. Why not just use strip()?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-05-2015 05:42 AM

1- strip() replaces these 2

2- It depends on the length of your variable. You'll have trailing spaces if your variable is longer than the text.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-05-2015 06:49 AM

Thanks RW9 and Chrish..Actually i was tring to just checking the result..It was fine too.

I used like

Y='$'||trim(left(x))||'$';-->$ab_cd$

Z='$'||left(trim(x))||'$';-->$ab_cd_$

These statement ware giving same out put for y and z for one of my friend.that was $ab_cd$.So I am not sure why there are diffetence in output.

I used like

Y='$'||trim(left(x))||'$';-->$ab_cd$

Z='$'||left(trim(x))||'$';-->$ab_cd_$

These statement ware giving same out put for y and z for one of my friend.that was $ab_cd$.So I am not sure why there are diffetence in output.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-05-2015 06:58 AM

Well, cant really tell from that. I ran these two and the output was the same $ab_cd$. Were you using different operating systems, versions of SAS, was it the same data, same options etc. Could be any number of things. For the code though, I would recommend using strip() as its simpler, and avoids reading the code differently. However, an even more simpler form would be cats():

data temp; x="ab_cd "; y="$"||trim(left(x))||"$"; z="$"||left(trim(x))||"$"; v="$"||strip(x)||"$"; w=cats("$",x,"$"); run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-05-2015 09:00 AM

You need to be specific about "the output". Sometimes SAS will strip blanks when it displays a value. You might consider using 'a0'x the so call hard space. Remember anytime you are looking a a value some kind of formatting has been done. To "see" the actual stored value use $HEX format.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-05-2015 09:25 AM

You should expect different results. The correct version is trim(left(value)). Consider what happens if you try left(trim(value)). First, the TRIM function removes trailing blanks. Then the LEFT function takes the leading blanks and puts them at the end of the string. Better yet, take a look at the STRIP function that removes both leading and trailing blanks.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

10-05-2015 02:52 PM

thanks all.

Actually I got the reason.

if we store the results in a variable a and apply any appending special character causing the same output.

If we assign the values to a variable results to write in data set and filling back the length by 8 characters hence same output.

And if apply appending a special character before storing to any variable (writing back to data set) results the different out put.

For above explanation,

data temp;

x="ab_cd ";

y="$"||trim(left(x))||"$";

z="$"||left(trim(x))||"$";

v="$"||strip(x)||"$";

w=cats("$",x,"$");

put y=;

put z=;

put v;

put w;

run;

y=$ab_cd$

z=$ab_cd $

$ab_cd$

$ab_cd$

Devi...

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-05-2015 03:27 PM

Do you see that your original question does not represent what you are actually doing?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-05-2015 04:18 PM

Hi. Be careful with character variable lengths.

The results of using character functions all have default lengths.

If you run PROC CONTENTS on data set TEMP, you'll see that the variable W created with the CATS has a length of 200 and that none of the other functions (TRIM or STRIP) change the length of the resulting variables V, Y, and Z.

The lengths will still be the length of any variables involved in the concatenation plus the lengths of any character constants. In this example that's 8, not because that's the default lenght of a character variabe, but because it's 6 (length of X) plus 2 (those two $);

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-06-2015 05:03 AM

Hi sorry i put the another code to reference my explanation.

Here is it.

Here is it.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-06-2015 05:03 AM

73 data temp;

74 x=" ab cd ";

75 y=left(trim(x));

76 z=trim(left(x));

77 y1='$'||left(trim(x))||'$';

78 z1='$'||trim(left(x))||'$';

79 y2='$'||y||'$';

80 z2='$'||z||'$';

81

82 put y1=;

83 put z1=;

84 put y2=;

85 put z2=;

86

87 run;

y1=$ab cd $

z1=$ab cd$

y2=$ab cd $

z2=$ab cd $

74 x=" ab cd ";

75 y=left(trim(x));

76 z=trim(left(x));

77 y1='$'||left(trim(x))||'$';

78 z1='$'||trim(left(x))||'$';

79 y2='$'||y||'$';

80 z2='$'||z||'$';

81

82 put y1=;

83 put z1=;

84 put y2=;

85 put z2=;

86

87 run;

y1=$ab cd $

z1=$ab cd$

y2=$ab cd $

z2=$ab cd $

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Devi

10-06-2015 05:13 AM

Sorry I put the wrong code above to explanation of my comments.

Below is it.

See below

Below is it.

See below