Help using Base SAS procedures

Cutting a String

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

Cutting a String

Hi Guys,

I have been trying to cut out a portion of the observations, but unsuccessful.

Basically, I have 2 digits, eg. 11, 12, 22, 31,... ....

I want to cut and isolate the first digit under a new variable named "Header"

So header will have 1, 1, 2, 3,

This is the code I used:

DATA CRSP;

SET CRSP;

Length HEADER $1.;

HEADER=SUBSTR(SHRCD,1);

RUN;

However, Header will have no observations.

Please Heap!

Thanks!


Accepted Solutions
Solution
‎12-07-2012 06:59 AM
Frequent Contributor
Posts: 106

Re: Cutting a String

Try

HEADER = put(SHRCD,best.-l) ;

If that works then a bunch of the assumptions that we had to make (since you provided no log) could be true.

Assumptions such as

  • SHRCD is numeric and not character
    So the log would have told us by mentioning 'Numeric values have been converted to character ...'
  • "Header will have no observations" should be "Header will be blank"
    Happens when SHRCD is numeric and the value is per default right justified in an eight byte field
    of which you extract (by means of SUBSTR) position one i.e. a blank

Now why would the above work without invoking function SUBSTR?

That's because the length of HEADER has already been declared as one byte. (Note BTW the statement has to be "length header $1", i.e. no dot after $1 . We are declaring the length here and not assigning a format.)

Transferring a longer character variable's value to a shorter variable results in the value being truncated at the shorter variable's length.

Oh, and  "best.-L" means: "write out the numeric value justified left in a format optimal for the field length".

Hope this heaps

Robert

View solution in original post


All Replies
Solution
‎12-07-2012 06:59 AM
Frequent Contributor
Posts: 106

Re: Cutting a String

Try

HEADER = put(SHRCD,best.-l) ;

If that works then a bunch of the assumptions that we had to make (since you provided no log) could be true.

Assumptions such as

  • SHRCD is numeric and not character
    So the log would have told us by mentioning 'Numeric values have been converted to character ...'
  • "Header will have no observations" should be "Header will be blank"
    Happens when SHRCD is numeric and the value is per default right justified in an eight byte field
    of which you extract (by means of SUBSTR) position one i.e. a blank

Now why would the above work without invoking function SUBSTR?

That's because the length of HEADER has already been declared as one byte. (Note BTW the statement has to be "length header $1", i.e. no dot after $1 . We are declaring the length here and not assigning a format.)

Transferring a longer character variable's value to a shorter variable results in the value being truncated at the shorter variable's length.

Oh, and  "best.-L" means: "write out the numeric value justified left in a format optimal for the field length".

Hope this heaps

Robert

Super User
Posts: 5,497

Re: Cutting a String

One more obscure possibility:  perhaps SHRCD is character, but contains leading blanks.  After the LENGTH statement, simply add:

HEADER = left(SHRCD);

Super User
Posts: 19,770

Re: Cutting a String

If shrcd is numeric and not character

header=floor(shrcd/10);

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 235 views
  • 6 likes
  • 4 in conversation