Help using Base SAS procedures

Align output printing to a specified character

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

Align output printing to a specified character

I have the following two variables and data:

var1                         align1

Indonesia       3

Japan           4

Panama          2

United Kingdom  5

I want to align the output of the the words, for example, to the 10th space of a column that is 20 spaces wide according to the number given by align1. So for Indonesia 3 means the 3rd character of the word, which means d, Japan 4 means the 4th character a, Panama 2 mean a, and United Kingdom 5 means e. So I want to output:

----|----|----|----|

       Indonesia

      Japan

        Panama

     United Kingdom

What output PROC can I use and what options should I have to achieve this?  I want to output to HTML and PDF, so any further assistance on this would be helpful.

Thanks!


Accepted Solutions
Solution
‎11-30-2012 04:55 AM
Contributor
Posts: 53

Re: Align output printing to a specified character

Putting together the code I received, this is the answer that works best:

Thanks to everyone!

data have;

length var1 $ 20;

input var1 $& align1;

_align=10-align1;

put @_align var1;

datalines;

Indonesia 3

Japan 4

Panama 2

United Kingdom 5

;

title; footnote;

ods listing;

ods pdf file ="c:\temp\useput.pdf" ;

ods html file="c:\temp\useput.html" ;

data have;

  set have;

  file print;

  put @_align var1;

  run;

ods _all_ close;

View solution in original post


All Replies
Respected Advisor
Posts: 4,659

Re: Align output printing to a specified character

You can do it like this :

data have;
length var1 $ 20;
input var1 $& align1;
datalines;
Indonesia       3
Japan           4
Panama          2
United Kingdom  5
;

%let alignPos=10;

proc sql;
create table want as
select cat(repeat(" ", &alignPos.-(max(align1)-align1)), var1) as var
from have;
select * from want;
quit;

PG

PG
Respected Advisor
Posts: 3,124

Re: Align output printing to a specified character

You may just simply do this:

data have;

length var1 $ 20;

input var1 $& align1;

_align=10-align1;

put @_align var1;

datalines;

Indonesia 3

Japan 4

Panama 2

United Kingdom 5

;

Haikuo

SAS Super FREQ
Posts: 8,744

Re: Align output printing to a specified character

Hi:
The OP said the interest was ODS HTML and/or ODS PDF. The look and feel of the output for those 2 destinations will be different than for LISTING output. Without getting into the issues of writing the output at a variable location (possible). I want to show what the output will look like in PDF and HTML compared to LISTING. If this is what the OP wants, then the ODS "sandwich" would need to be added around your DATA step program and add the FILE PRINT statement.

cynthia

ods listing;

ods pdf file="c:\temp\useput.pdf";

ods html file="c:\temp\useput.html";

data have;

  ** SET statement would go here or INFILE;

file print;

  put @1 '0........1....1....2';

  put @1 '12345....0....5....0';

  put @3 'Indonesia';

  put @4 'Japan';

  put @2 'Panama';

  put @5 'United Kingdom';

run;

ods _all_ close;


file_print_with_put.png
Contributor
Posts: 53

Re: Align output printing to a specified character

Thank you Hai.kuo. Your code was simple and it worked!

Thanks Cynthia for addressing the issues of ODS in PDF and HTML.

Thanks Ksharp for your answers too.

I really appreciate this forum. So many helpful people in such a short time!! Thanks again to all.

Super User
Posts: 9,691

Re: Align output printing to a specified character

You can use escape char ~_ to instead of a blank in ODS.

data have;
length var1 $ 20;
input var1 $& align1;
datalines;
Indonesia       3
Japan           4
Panama          2
United Kingdom  5
China           1
;
run;
proc sql noprint; select max(align1) into : max from have;quit;
data want;
length var1 $ 100;
 set have;
 if  align1 ne &max then var1=cats(repeat('~_',&max-align1-1),var1);
run;
ods listing close;
ods pdf file="c:\temp\useput.pdf" style=sasweb;
ods html file="c:\temp\useput.html" style=sasweb;
ods escapechar='~';
proc print noobs;run;
ods pdf close;
ods html close;
ods listing;

Ksharp

Solution
‎11-30-2012 04:55 AM
Contributor
Posts: 53

Re: Align output printing to a specified character

Putting together the code I received, this is the answer that works best:

Thanks to everyone!

data have;

length var1 $ 20;

input var1 $& align1;

_align=10-align1;

put @_align var1;

datalines;

Indonesia 3

Japan 4

Panama 2

United Kingdom 5

;

title; footnote;

ods listing;

ods pdf file ="c:\temp\useput.pdf" ;

ods html file="c:\temp\useput.html" ;

data have;

  set have;

  file print;

  put @_align var1;

  run;

ods _all_ close;

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 205 views
  • 9 likes
  • 5 in conversation