BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
japelin
Rhodochrosite | Level 12

I would like to output the data in the following format, where each observation is independent and the content of one observation is broken into three lines.
The output is supposed to be an rtf or word file.

japelin_0-1659673565996.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

Thank you all very much.
I have solved the problem and here is the sample code.

The part where the layout is adjusted and output is PROC REPORT, and the subsequent fine tuning was handled by rewriting the RTF control code.

 

ods _all_ close;
ods noresults;
filename rtf_in "%sysfunc(pathname(work))\rtf_in.rtf";
filename rtf_out "%sysfunc(pathname(work))\rtf_out.rtf";
ods rtf file=rtf_in;
title;
options nodate nonumber nobyline;
proc sort data=sashelp.class out=class;
  by name;
run;
proc report data=class nowd;
  column name NameLabel sex age height weight;
  by name;
  define NameLabel / ''
                     computed 
                     style(header)=[just=l
                                    backgroundcolor=white
                                    BorderTopColor=White
                                    BorderLeftColor=White
                                    BorderRightColor=White
                                    BorderBottomColor=White];
  define name / noprint;
  compute NameLabel / character length=40;
    call define(_col_,'style','style={just=l 
                                      fontweight=bold
                                      fontsize=14pt
                                      cellwidth=15cm
                                      BorderTopColor=White
                                      BorderLeftColor=White
                                      BorderRightColor=White
                                      BorderBottomColor=White}');
    NameLabel ='Name: '||name;
  endcomp;
  compute sex;
    call define(_col_,'style','style={just=l cellwidth=7.5cm}');
  endcomp;
  compute age;
    call define(_col_,'style','style={just=l cellwidth=7.5cm}');
  endcomp;
  compute height;
    call define(_col_,'style','style={just=l cellwidth=7.5cm}');
  endcomp;
  compute weight;
    call define(_col_,'style','style={just=l cellwidth=7.5cm}');
  endcomp;
run;
ods rtf close;

data _null_;
  infile rtf_in;
  file rtf_out;
  input;
  if _infile_='\pard\par' then _infile_='\pard';
  else if index(_infile_,'\sect') then delete;
  put _infile_;
run;

 

Results:

japelin_1-1661007790918.png

 

View solution in original post

8 REPLIES 8
andreas_lds
Jade | Level 19

Hardly possible to suggest something without knowing how the data looks like. So please post an excerpt of the data you have in usable form: a data step using datalines statement.

 

japelin
Rhodochrosite | Level 12

Thanks.

It is a plain wide data set.

 

data have;
  length var1 8 var2 -var7 $200;
  format var1 yymmdd10.;
  input var1:yymmdd10. var2-var7;
datalines;
2022-08-01 TypeA 0001 X-01 AAAAAAA BBBBBBBBB SERVERX
2022-08-05 TypeB 0002 X-99 AAAAAAA BBBBBBBBB SERVERX
2022-08-05 TypeA 0004 X-80 AAAAAAA BBBBBBBBB SERVERY
2022-08-10 TypeA 0010 X-30 AAAAAAA BBBBBBBBB SERVERX
;
run;
andreas_lds
Jade | Level 19

I hoped that var7 has a unique value in each observation, so that proc report could be used, but with the data shown, i don't think that proc report could be used, but i am not super-familiar with the proc.

 

Report Writing Interface could be used, but you will have to write statement for anything you want to see and even for some things you don't want to see. Here's on possible approach:

%let dummy_header = %str(backgroundcolor=colors('headerbg') color=colors('titlefg') );


data _null_;
   set work.have end=jobDone;

   length _dummy $ 200;

   if _n_ = 1 then do;
      declare odsout rwi();
   end;

   rwi.table_start();

   _dummy = catx(': ', vlabel(var7), var7);
   rwi.row_start(type: 'h');
   rwi.format_cell(data: _dummy, inhibit: 'x', column_span: 4, just: 'L');
   rwi.row_end();

   rwi.row_start(type: 'h');
   rwi.format_cell(data: vlabel(var1));
   rwi.format_cell(data: vlabel(var2));
   rwi.format_cell(data: vlabel(var3));
   rwi.format_cell(data: vlabel(var4));
   rwi.row_end();

   rwi.row_start();
   rwi.format_cell(data: var1, just: 'R');
   rwi.format_cell(data: var2, just: 'R');
   rwi.format_cell(data: var3, just: 'R');
   rwi.format_cell(data: var4, just: 'R');
   rwi.row_end();

   rwi.row_start();
   rwi.format_cell(data: vlabel(var5), column_span: 4, just: 'L', style_attr: "&dummy_header.");
   rwi.row_end();
   rwi.row_start();
   rwi.format_cell(data: var5, column_span: 4, just: 'L');
   rwi.row_end();

   rwi.row_start();
   rwi.format_cell(data: vlabel(var6), column_span: 4, just: 'L', style_attr: "&dummy_header.");
   rwi.row_end();
   rwi.row_start();
   rwi.format_cell(data: var6, column_span: 4, just: 'L');
   rwi.row_end();

   rwi.table_end();

run;
japelin
Rhodochrosite | Level 12

Thankyou! It works.

 

If there is a unique variable like below, can it be handled by Proc report?

data have;
  length key 8 var1 8 var2 -var7 $200;
  format var1 yymmdd10.;
  input key var1:yymmdd10. var2-var7;
datalines;
1 2022-08-01 TypeA 0001 X-01 AAAAAAA BBBBBBBBB SERVERX
2 2022-08-05 TypeB 0002 X-99 AAAAAAA BBBBBBBBB SERVERX
3 2022-08-05 TypeA 0004 X-80 AAAAAAA BBBBBBBBB SERVERY
4 2022-08-10 TypeA 0010 X-30 AAAAAAA BBBBBBBBB SERVERX
;
run;
andreas_lds
Jade | Level 19

Var7 needs to be unique, so that by var7 can be used in proc report. That should create a single table for each value. I still don't know how to include the variables label in the output of the byline, and, because i haven't used compute-blocks in proc report, i still have no idea how they could be defined to print var5 and var6 as requested.

 

Maybe @Cynthia_sas has an idea.

Ksharp
Super User
data have;
length sex $ 40;
 set sashelp.class;
 var6=name;
 var7=sex;
run;
data have2;
 set have;
 output;
 call missing(sex ,age, weight, height);
 sex='Label of Var5';output;
 sex=var6;output;

 sex='Label of Var6';output;
 sex=var7;output;
run;
proc sort data=have2;by name;run;

options nodate nonumber missing=' ';
title;
ods rtf file='c:\temp\temp.rtf' startpage=no;
proc report data=have2 nowd style={outputwidth=100% } 
style(header column)={ font_weight=light 
 bordertopcolor=black bordertopwidth=2 borderbottomcolor=black borderbottomwidth=2 };
column name sex age weight height var6 var7;
define name/order noprint ;
define var6/display noprint;
define var7/display noprint;
compute var7;
n+1;
if mod(n,2)=0 then do;
call define(_row_,'style','style=header{font_weight=light}');
call define('_C3_','style','style=header{borderleftcolor=graybb borderleftwidth=2 borderrightcolor=graybb borderrightwidth=2}');
call define('_C4_','style','style=header{borderleftcolor=graybb borderleftwidth=2 borderrightcolor=graybb borderrightwidth=2}');
end;
if mod(n,2)=1 and mod(n,3) ne 1 then do;
call define('_C3_','style','style={borderleftcolor=white borderleftwidth=2 borderrightcolor=white borderrightwidth=2}');
call define('_C4_','style','style={borderleftcolor=white borderleftwidth=2 borderrightcolor=white borderrightwidth=2}');
end;
endcomp;

compute before _page_/style={just=l bordertopcolor=white bordertopwidth=2
borderleftcolor=white borderleftwidth=2 borderrightcolor=white borderrightwidth=2};
line 'Name:' name $40.;
endcomp;
break after name/page;
run;
ods rtf close;

Ksharp_0-1659700779691.png

 

japelin
Rhodochrosite | Level 12

Thank you all very much.
I have solved the problem and here is the sample code.

The part where the layout is adjusted and output is PROC REPORT, and the subsequent fine tuning was handled by rewriting the RTF control code.

 

ods _all_ close;
ods noresults;
filename rtf_in "%sysfunc(pathname(work))\rtf_in.rtf";
filename rtf_out "%sysfunc(pathname(work))\rtf_out.rtf";
ods rtf file=rtf_in;
title;
options nodate nonumber nobyline;
proc sort data=sashelp.class out=class;
  by name;
run;
proc report data=class nowd;
  column name NameLabel sex age height weight;
  by name;
  define NameLabel / ''
                     computed 
                     style(header)=[just=l
                                    backgroundcolor=white
                                    BorderTopColor=White
                                    BorderLeftColor=White
                                    BorderRightColor=White
                                    BorderBottomColor=White];
  define name / noprint;
  compute NameLabel / character length=40;
    call define(_col_,'style','style={just=l 
                                      fontweight=bold
                                      fontsize=14pt
                                      cellwidth=15cm
                                      BorderTopColor=White
                                      BorderLeftColor=White
                                      BorderRightColor=White
                                      BorderBottomColor=White}');
    NameLabel ='Name: '||name;
  endcomp;
  compute sex;
    call define(_col_,'style','style={just=l cellwidth=7.5cm}');
  endcomp;
  compute age;
    call define(_col_,'style','style={just=l cellwidth=7.5cm}');
  endcomp;
  compute height;
    call define(_col_,'style','style={just=l cellwidth=7.5cm}');
  endcomp;
  compute weight;
    call define(_col_,'style','style={just=l cellwidth=7.5cm}');
  endcomp;
run;
ods rtf close;

data _null_;
  infile rtf_in;
  file rtf_out;
  input;
  if _infile_='\pard\par' then _infile_='\pard';
  else if index(_infile_,'\sect') then delete;
  put _infile_;
run;

 

Results:

japelin_1-1661007790918.png

 

Tom
Super User Tom
Super User

Looks like a job for PROC FORMS or PROC FSLETTER.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 644 views
  • 1 like
  • 4 in conversation