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?
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;
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"?
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.
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.
@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;
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.
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;
Yes!!! This works!!! Thank you!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: