What would you expect the output of this code to be? What about the value of x in the 'v2' put statement?
data _null_;
length x $200;
x='?';
substr(x,70,1)='!';
put 'v1' x= $char200.;
x=' ';
substr(x,60,8)='la chose';
put 'v2' x= $char200.;
run;
For the second one, I expected 59 blanks followed by the text. This expectation was apparently in error. How could I have predicted the observed behavior (X='la chose' with no leading blanks)?
Please don't ask me what I'm trying to do. I already solved that; now I'm just curious as to how SUBSTR= works with blanks. The SUBSTR (left side of 😃 page of the SAS documentation did not specify this behavior.
@mftuchman wrote:
What would you expect the output of this code to be? What about the value of x in the 'v2' put statement?
data _null_; length x $200; x='?'; substr(x,70,1)='!'; put 'v1' x= $char200.; x=' '; substr(x,60,8)='la chose'; put 'v2' x= $char200.; run;
For the second one, I expected 59 blanks followed by the text. This expectation was apparently in error. How could I have predicted the observed behavior (X='la chose' with no leading blanks)?
Please don't ask me what I'm trying to do. I already solved that; now I'm just curious as to how SUBSTR= works with blanks. The SUBSTR (left side of 😃 page of the SAS documentation did not specify this behavior.
X= in the PUT statement specifies NAMED-PUT leading/trailing blanks are trimmed.
data _null_;
length x $200;
x='?';
substr(x,70,1)='!';
put 'v1' x $char200.;
x=' ';
substr(x,60,8)='la chose';
put 'v2' x $char200.;
run;
@mftuchman wrote:
What would you expect the output of this code to be? What about the value of x in the 'v2' put statement?
data _null_; length x $200; x='?'; substr(x,70,1)='!'; put 'v1' x= $char200.; x=' '; substr(x,60,8)='la chose'; put 'v2' x= $char200.; run;
For the second one, I expected 59 blanks followed by the text. This expectation was apparently in error. How could I have predicted the observed behavior (X='la chose' with no leading blanks)?
Please don't ask me what I'm trying to do. I already solved that; now I'm just curious as to how SUBSTR= works with blanks. The SUBSTR (left side of 😃 page of the SAS documentation did not specify this behavior.
X= in the PUT statement specifies NAMED-PUT leading/trailing blanks are trimmed.
data _null_;
length x $200;
x='?';
substr(x,70,1)='!';
put 'v1' x $char200.;
x=' ';
substr(x,60,8)='la chose';
put 'v2' x $char200.;
run;
[to be deleted]
Consider
data junk; length x $200; x='?'; substr(x,70,1)='!'; put 'v1' x= $char200.; x=' '; substr(x,60,8)='la chose'; l = length(x); run;
If you open the data set junk with any of the SAS viewers you will see a bunch of leading spaces. The value of L will count the leading spaces plus the actual text ending at the 'e' in 'chose'.
Given that you seem to know what you are doing I am not sure I follow what the question is.
SAS stores all character variables as fixed lengths strings that are padded with spaces. So if you start with a missing/null/empty variable of length 200 it has 200 spaces in it. If you then change 70th position into an X it now has 69 spaces one X and then 130 spaces.
The question was originally "why do my leading spaces disappear?". Is it possible that SUBSTR= does not do what I think it does? But it does; it was the named PUT that threw me off. When I surrounded it with delimiters, the spaces were visible.
And I have to remember that single quote works for SAS, but not for English grammar 🙂
Not trying to replace X - just trying to visualize a sparse vector in plain text, so I want to build a long blank string and then populate segments of it using substr=.
Turns out I got an easier to read result by using a non-destructive function CATX(OF ....) to build my string.
Hope that background helps a litt.e
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.