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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

7 REPLIES 7
mkeintz
PROC Star

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"?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Shmuel
Garnet | Level 18

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.

acairns
Fluorite | Level 6

Unfortunately, that doesn't work. 

It outputs:

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

 

Thank you for your response though 🙂

 

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.

Shmuel
Garnet | Level 18

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

ballardw
Super User

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; 
acairns
Fluorite | Level 6

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1622 views
  • 0 likes
  • 5 in conversation