07-25-2017 09:58 AM
I was wondering how the length of the BLANKSTR function is set. When I create a vector out of a loop like in the following code:
PROC IML; vector = J(5,1,BlankStr(11)); DO i=1 TO 5; vector[i,1] = char(i) + 'M'; END; CREATE vector FROM vector; APPEND FROM vector; QUIT;
The resulting table "vector" is empty. If I change the argument in BLANKSTR to 13, however, I get the expected result. Do I use BLANKSTR incorrectly or what is behind this?
07-25-2017 10:43 AM
I don't know what "expected result" you are seeing (not seeing?), but you can use the NLENG function to see the length of a character matrix. That is the length of the variable that is written to the SAS data set. Run the following, which should show that the SAS/IML vector contains 11 characters per element and that 11 is the length of the data set variable:
PROC IML; vector = J(5,1,BlankStr(11)); L = nleng(vector); print L; DO i=1 TO 5; vector[i] = char(i) + 'M'; END; CREATE vector var "vector"; APPEND; CLOSE; QUIT; ods select Variables; proc contents data=vector; run;
07-25-2017 10:48 AM
Is the issue here the default length of the string returned by the CHAR function? If the default is 12, then each string will have 11 leading blanks before the number. Try using strip(char(i)) instead of char(i) to remove the blanks.
07-25-2017 11:15 AM
You cannot put more than 11 characters into a character variable that is defined as length $11.
If you let SAS autoconvert a number into a character string for you it will default to using the BEST12. format.
So the value of 1 will become a string of 11 blanks followed by the digit '1'. If you then put that into a variable that is only 11 characters long all you have left are the blanks.