BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mftuchman
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

@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;

 

View solution in original post

7 REPLIES 7
data_null__
Jade | Level 19

@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
Quartz | Level 8

[to be deleted]

 

ballardw
Super User

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'.

Tom
Super User Tom
Super User

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.

mftuchman
Quartz | Level 8

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 🙂

 

 

Astounding
PROC Star
SUBSTR= does not replace X. Instead, it replaces some of the characters in X (in this case, characters 60 through 67), leaving any other characters in X unchanged.
mftuchman
Quartz | Level 8

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

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2635 views
  • 4 likes
  • 5 in conversation