Statistical programming, matrix languages, and more

Length of BLANKSTR() function

Reply
Occasional Contributor
Posts: 7

Length of BLANKSTR() function

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?

 

Thank you!

SAS Super FREQ
Posts: 3,752

Re: Length of BLANKSTR() function

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;
Frequent Contributor
Posts: 147

Re: Length of BLANKSTR() function

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.

Super User
Super User
Posts: 7,042

Re: Length of BLANKSTR() function

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.

Ask a Question
Discussion stats
  • 3 replies
  • 220 views
  • 2 likes
  • 4 in conversation