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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Robert_Bardos
Fluorite | Level 6

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

3 REPLIES 3
Robert_Bardos
Fluorite | Level 6

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

Astounding
PROC Star

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

HEADER = left(SHRCD);

Reeza
Super User

If shrcd is numeric and not character

header=floor(shrcd/10);

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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