- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
--------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes!!! This works!!! Thank you!