DATA Step, Macro, Functions and more

Want to add a space at the end of a set of concatenated variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Want to add a space at the end of a set of concatenated variables

Hello!

 

I am trying to combine a set of variables and add a space at the end so that the next set of variables will be separated.  Basically, I am creating a proc contents that will be used as a put statement for the output.

 

type = (trim(name)||' '||trim(format)||trim(lengthx)||'.'||' ');

 

The output should read:

a $10. b $3. d $3. e YYMMDD8. f $3.

 

But this is what I'm getting:

a $10.b $3.d $3.e YYMMDD8.f $3.

 

It is dropping the last ' '.

 

Any hints?


Accepted Solutions
Solution
‎12-07-2016 11:24 AM
Super User
Posts: 10,487

Re: Want to add a space at the end of a set of concatenated variables

It really helps to show your entire code. Since neither the desired or actual result can be achieved with a single pass across a name format pair you obviously have some kind of loop.

You may be falling afoul of something other than the line of code show, likely how you concatenate the previous and current iterations of type.

 

Please look at this for an alternate approach to what I think you are doing.

data have;
   informat name $32. format $25. ;
   input name format lengthx;
datalines;
a $ 10
b $ 3
d $ 3
e yymmdd 8
f $ 3
;
run;

data want;
   set have;
   length str $ 200 type $ 50;
   retain str;
   type = catx(' ',name, cats(format,lengthx,'.'));
   str = catx(' ',str,type);
run; 

View solution in original post


All Replies
Valued Guide
Posts: 797

Re: Want to add a space at the end of a set of concatenated variables

SAS has a collection of concatenation functions.  You want CATX, as in

 

type=catx(' ',name,format,lengthx||'.');

 

 

What sort of statement are you using to print out the values?   "put"?

Trusted Advisor
Posts: 1,372

Re: Want to add a space at the end of a set of concatenated variables

You cannot see the last space as it becomes part of of the line.

The only way to see it is by adding a vissible character after it.

Occasional Contributor
Posts: 11

Re: Want to add a space at the end of a set of concatenated variables

[ Edited ]

Unfortunately, that doesn't work. 

It outputs:

 a $ 10 .b $ 3 .d $ 3 .e YYMMDD 8 .f $ 3 .

 

Thank you for your response though Smiley Happy

 

We are trying to create a text file where the records will have different variables.  It is unusual but the data is being uploaded to the Peoples' Bank of China and this is part of their requirements.  Essentially it is a variable fixed-width file.

Trusted Advisor
Posts: 1,372

Re: Want to add a space at the end of a set of concatenated variables

@acairns, it is difficult to understand where is your problem without having test data,

so I have created one just to demostrate the CATX function and its result.

Run next code and chek A_LINE result in log:

 

data _NULL_;
length a_line $80;
retain a_line;
infile datalines truncover;
input name $ format $ lengthx $;
if name = 'ZZZ' then put a_line=;
a_line = catx(' ',a_line, name, trim(format)||'.');

datalines;
a $10
b $3
d $3
e yymmdd8
f $3
ZZZ       /* assigning end of input */
; run;

Super User
Super User
Posts: 7,401

Re: Want to add a space at the end of a set of concatenated variables

Is there a need to do it like that?  Just thinking, if you have the dataset then you could simply:

data _null_;
  set sashelp.vcolumn (where=(libname="WORK" and memname="ABC")) end=last;
  if _n_=1 then call execute('data _null_; file "c:\abc.txt"; set work.abc; put ');
  call execute(cat(" ",name,ifc(type="char","$",""),strip(put(length,best.)),"."));
  if last then call execute('; run;');
run;

Obviously the ifc() above only handles num/char, you might need another ifc() to handle formats if needed.

Solution
‎12-07-2016 11:24 AM
Super User
Posts: 10,487

Re: Want to add a space at the end of a set of concatenated variables

It really helps to show your entire code. Since neither the desired or actual result can be achieved with a single pass across a name format pair you obviously have some kind of loop.

You may be falling afoul of something other than the line of code show, likely how you concatenate the previous and current iterations of type.

 

Please look at this for an alternate approach to what I think you are doing.

data have;
   informat name $32. format $25. ;
   input name format lengthx;
datalines;
a $ 10
b $ 3
d $ 3
e yymmdd 8
f $ 3
;
run;

data want;
   set have;
   length str $ 200 type $ 50;
   retain str;
   type = catx(' ',name, cats(format,lengthx,'.'));
   str = catx(' ',str,type);
run; 
Occasional Contributor
Posts: 11

Re: Want to add a space at the end of a set of concatenated variables

Yes!!!  This works!!!  Thank you!

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 223 views
  • 0 likes
  • 5 in conversation