1

I have the following temp dataset with 2 observations. The second observation contains a trailing blank.In the data _null_ step I read the value from the second observation with call symput. In the put statement the value is written to the log with 6 blanks: TEXT=Test controltext.

How can I achieve that TEXT=Test controltext is printed? In temp dataset are some observation with trailing blank and some with none. So I do not know if there is a trailing blank...

data temp;
  a="MyTesttext";
  output;
  a="Test ";
  output;
run;

data _null_;
  set temp;
  if _n_ = 2 then do;
    call symput('text',a);
  end;
run;

%put &=text.controltext;
zuluk
  • 1,557
  • 8
  • 29
  • 49
  • There six trailing blanks after 'Test' in the variable A, because the variable A has a length of 10. Is your goal to preserve one trailing blank in the value of the macro variable &text? – Quentin Nov 03 '17 at 08:55
  • Correct, I want to preserve the trailing blank which is written to variable a. – zuluk Nov 03 '17 at 09:05
  • In another comment you said if there were two trailing blanks, you want to preserve them. In this case there are 6 trailing blanks not one. But you only want to preserve one? If the value assigned to A was "text " would you want to preserve two blanks? Since A="text " and A="text " both have six trailing blanks, you can't distinguish between them. – Quentin Nov 03 '17 at 09:20
  • I want to preserve the blanks given in the string at `temp`.In the above example there is one blank. If there are 2 or more blanks I want to preserve these blanks. – zuluk Nov 03 '17 at 09:25

2 Answers2

2

In the DATA step language, in a character variable if the length of a value is less than the length of the variable, it is padded with trailing blanks. When you code a="Test " (one trailing blank) or a="Test " (three trailing blanks) and the variable A is defined with a length of $10, they are both stored with six trailing blanks.

So:

data _null_;
  length a $10;
  a="Test ";
  call symput('TextOneBlank',a);
  a="Test   ";
  call symput('TextThreeBlanks',a);
run;

%put >>&TextOneBlank<<;
%put >>&TextThreeBlanks<<;

returns:

75   %put >>&TextOneBlank<<;
>>Test      <<
76
77   %put >>&TextThreeBlanks<<;
>>Test      <<

One way to preserve a fixed number of trailing blanks is to add your own delimiter to the text value, so that they are not actually trailing blanks. For example, you can use a pipe as a delimiter:

data _null_;
  length a $10;
  a="|Test |";
  call symput('TextOneBlank',scan(a,1,'|'));
  a="|Test   |";
  call symput('TextThreeBlanks',scan(a,1,'|'));
run;

which will return:

86   %put >>&TextOneBlank<<;
>>Test <<
87   %put >>&TextThreeBlanks<<;
>>Test   <<
Quentin
  • 5,960
  • 1
  • 13
  • 21
1

Your put statement result contains "extra" blanks because, in your first datastep, you implicitely initialize variable a with a length of 10 (the length of "MyTesttext").

Once variable a of length 10 is assigned the value "Test ", that extra trailing blank is essentially lost. You can observe that by running the following:

data _null_;
  a="MyTesttext";
  put '"' a +(-1) '"';
  a="Test ";
  put '"' a +(-1) '"';
run;

The result in the log is

"MyTesttext"
"Test"

The extra blank is gone.

You could achieve the results you describe by using compbl(), but that would only be correct in this particular case where you had only one trailing blank.

user2877959
  • 1,792
  • 1
  • 9
  • 14
  • Hmm, so I have to specify: If I have 2 trailing blanks I also would like to preserve them. This is with `compbl()` not possible as you already mentioned. – zuluk Nov 03 '17 at 09:09
  • When outputting in the datastep with `put` another approach is to append something and then strip it off as follows by moving the cursor back 2 characters: `a=cat("Test ", '|');` `put '"' a +(-2) '"';` – Hamish Carpenter May 29 '18 at 23:19