Desktop productivity for business analysts and programmers

Add one blank (space) at the end of a character var, an then export to .txt

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Add one blank (space) at the end of a character var, an then export to .txt

Hi guys,

 

 Can someone help please?

 

 How can I add an extra blank at the end of a character variable and then export it to a .TXT file with the blank at the end.

 

 I have tried to concatenate name = name || " "; but doesn't work. Please see the code below (just copy and paste):

 

 

 The problem is to add an extra blank, the proc export is working fine, but without the extra blank at the end.

 

Thanks and regards!!

 

data have1;
input name & $2.;
datalines;
a
;
run;

data have1;
set have1;
name = name || " ";
run;

PROC EXPORT DATA = have1 OUTFILE = '/sas/teste.TXT' REPLACE ;
PUTNAMES=NO;
RUN;

Accepted Solutions
Solution
‎04-21-2016 04:35 PM
Respected Advisor
Posts: 4,972

Re: Add one blank (space) at the end of a character var, an then export to .txt

It's not too difficult to program it yourself:

 

data _null_;

set have;

file '/sas/teste.txt' noprint;

put name $3.;

run;

 

There's still a question of delimiters between fields, which fields you really need to write out, whether to add a blank after numerics, etc.  But you can program it yourself.

 

You can't add a blank to the end of the variable NAME, because its length doesn't change.  Given that NAME is defined as having a length of 2, this statement will not change the value of NAME:

 

name = name || 'abcde';

 

There's only room for 2 characters, so "abcde" doesn't fit and gets truncated.

View solution in original post


All Replies
Solution
‎04-21-2016 04:35 PM
Respected Advisor
Posts: 4,972

Re: Add one blank (space) at the end of a character var, an then export to .txt

It's not too difficult to program it yourself:

 

data _null_;

set have;

file '/sas/teste.txt' noprint;

put name $3.;

run;

 

There's still a question of delimiters between fields, which fields you really need to write out, whether to add a blank after numerics, etc.  But you can program it yourself.

 

You can't add a blank to the end of the variable NAME, because its length doesn't change.  Given that NAME is defined as having a length of 2, this statement will not change the value of NAME:

 

name = name || 'abcde';

 

There's only room for 2 characters, so "abcde" doesn't fit and gets truncated.

Trusted Advisor
Posts: 1,114

Re: Add one blank (space) at the end of a character var, an then export to .txt

Hi @hy86,

 

--- I see, @Astounding was faster ---

 

Please note that your second data step is redundant in two ways:

  1. Variable NAME has length 2 (thanks to your informat specification $2.), hence it contains already the desired trailing blank after 'a'.
  2. Whatever you tried to append using 
    name = name || "whatever"
    it would not appear in variable NAME, because the expression right of the equals sign would append it after the last character of NAME (i.e. after the trailing blank), but there is no room for a third, fourth, ... character in variable NAME (left of the equals sign) because of its length 2.

The problem with PROC EXPORT is that this procedure uses (what is called) list output, as can be seen in the log of the PROC step:

put name $ ;

This will always shed trailing blanks.

 

What you need is formatted output:

put name $2.;

So, you could use the DATA _NULL_ step from the log of PROC EXPORT as a basis and modify it to use formatted output, as shown above.

Grand Advisor
Posts: 9,576

Re: Add one blank (space) at the end of a character var, an then export to .txt

You can make both blank and | as delimiter :

 

data _null_;
set sashelp.class;
file '/folders/myfolders/xx.txt' dlmstr=' |';
put (_all_) (:);
run;
Esteemed Advisor
Posts: 5,198

Re: Add one blank (space) at the end of a character var, an then export to .txt

Odd request. What kind of receiving system needs a trailing blank to do it's import?

I think interchanged data should be as clean as possible, and adding extra blanks generally doesn't add any value to the data.

Data never sleeps
Occasional Contributor
Posts: 16

Re: Add one blank (space) at the end of a character var, an then export to .txt

The recieving system will use the .txt file to update a table on the mainframe, so when the program reads blank it wont change that specific info, when it is <> from blank he updates the record. Don't know exactly why it works like that, but that the way it is.

Thanks and regards!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 492 views
  • 4 likes
  • 5 in conversation