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

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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