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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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