BookmarkSubscribeRSS Feed
philip_
Fluorite | Level 6

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!

3 REPLIES 3
Rick_SAS
SAS Super FREQ

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;
IanWakeling
Barite | Level 11

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.

Tom
Super User Tom
Super User

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.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 1460 views
  • 2 likes
  • 4 in conversation