Search code examples
sassas-macro

Why can't I use length function inside input function in SAS?


I have a code that converts a character to numeric using the informat and I'm using length function as the value of informat.

However, I'm having error with this approach.

Background of this problem is that the informat before was fixed value. I want to enhance the code for the informat to be flexible and remove the fixed value.

Before code:

data work.test;
emp_input = '168643123'
emp_value = input(emp_input, 6.);
run;

My current testcode:

data work.test;
emp_input = '168643123'
emp_value = input(emp_input, length(emp_input).);
run;

I expect the result that character '168643123' would be converted to numeric 168643123.

Using before code the output for this would be: numeric 168643.


Solution

  • That is not valid syntax. You have to use inputn an and then generate an string for the format.

    data work.test;
      emp_input = '168643123';
      emp_value = inputn(emp_input, cats(put(length(emp_input),3.),'.'));
    run;
    

    But better use Use BEST32. for all generic numbers of up 32 chars length.

    data work.test;
      emp_input = '168643123';
      emp_value = input(emp_input, BEST32.);
    run;