Reputation: 63
I have a table as below:
PROC_TP
112
5255
5255
5255
112
5255
5255
The PROC_TP is numeric with format 6. and I only want to get the first digit so I changed it into character using the put function as:
char_PROC_TP=put(PROC_TP,6.);
format char_PROC_TP $6.;
Then the table looks like this:
PROC_TP Char_PROC_TP
112 112
5255 5255
5255 5255
5255 5255
112 112
5255 5255
5255 5255
Now the char_PROC_TP is character with format of $6. Then I used the substr function as:
first_digit=substr(char_PROC_TP,1,1);
Then I don't get any value. So, can anyone tell me what could be the issue with it and its solution. Any help will be appreciated.
Upvotes: 1
Views: 110
Reputation: 21274
substrN() will take a numeric argument and automatically convert it to character and trim leading and trailing blanks, as necessary.
First = SUBSTRN(num_var, 1, 1);
See the documentation for SUBSTRN.
Upvotes: 4
Reputation: 28411
Use the STRIP function
char_PROC_TP=strip(put(PROC_TP,6.));
data test;
Length FIRST_DIGIT $1;
input PROC_TP;
char_proc_tp = strip(put(proc_tp,6.));
first_digit=substr(char_PROC_TP,1,1);
datalines;
112
5255
5255
5255
112
5255
5255
;
Upvotes: 0
Reputation: 9109
This is happening because your conversion to character is creating a right justified value. You can fix it with the justification option on the 6. format as 6.-L. Also for the first character use the first function it default to length $1.
data test;
input PROC_TP;
char_proc_tp = put(proc_tp,6.-L);
first_digit=first(char_PROC_TP);
cards;
112
5255
5255
5255
112
5255
5255
;;;;
run;
Upvotes: 1