Hi,
I am trying to use PUT and column pointer controls to output a table in Markdown format.
I have set SAS to use UTF-8; whenever I encounter a string with an accented character, the column pointer control comes up one character short and the row becomes misaligned.
example:
data have; input id name $; datalines; 35 Ontario 24 Québec 46 Manitoba ; run; data _null_; set have; put @32 '|' @1 '|' @3 name @12 '|' @14 id; run;
Results in:
| Ontario | 35 | | Québec | 24 | | Manitoba | 46 |
I get why that happens (accented characters are multibyte) and it's just a minor nuiscance, however I would like to know if there is a UTF-8 safe way to use PUT and column pointers.
Thanks!
You will need to use relative cursor movement or calculated cursor positions to adjust for the different number of bytes being written.
data have;
length id 8 name $10 ;
input id name ;
offset=length(name)-klength(name);
datalines;
35 Ontario
24 Québec
46 Manitoba
;
data _null_;
set have;
put
@1 '| ' name
@12 +offset '| ' id
@32 +offset '|'
;
run;
87 data _null_; 88 set have; 89 put 90 @1 '| ' name 91 @12 +offset '| ' id 92 @32 +offset '|' 93 ; 94 run; | Ontario | 35 | | Québec | 24 | | Manitoba | 46 |
Hi:
I'm not encountering your issue. I always find it better to use explicit formats with my PUT and to work from left-to-right when writing my variable values. Here's what I get:
I adjusted the column start positions to account for a longer NAME value, such as Saskatchewan. I also wrote to a TXT file and to the SAS log and did not observe what you describe. If you continue to have issues, I would recommend opening a track with Tech Support.
Cynthia
You will need to use relative cursor movement or calculated cursor positions to adjust for the different number of bytes being written.
data have;
length id 8 name $10 ;
input id name ;
offset=length(name)-klength(name);
datalines;
35 Ontario
24 Québec
46 Manitoba
;
data _null_;
set have;
put
@1 '| ' name
@12 +offset '| ' id
@32 +offset '|'
;
run;
87 data _null_; 88 set have; 89 put 90 @1 '| ' name 91 @12 +offset '| ' id 92 @32 +offset '|' 93 ; 94 run; | Ontario | 35 | | Québec | 24 | | Manitoba | 46 |
@gabonzo wrote:
I like the idea, but it would get a bit clunky if I have to calculate offsets for multiple columns. I think I will try this anyway.
Thanks!
Not a good idea to try to create a fixed column text file unless you are using a single byte encoding. It is better to make a report using ODS. Or make a delimited data file, like a CSV file.
You just need to know how many fields before the current one could contain non single byte characters (basically how many character fields).
put
@1 field1
@10 +offset1 field2
@30 +offset1 +offset2 field3
...
;
I don't understand your larger issue. Markdown is a text markup language. Why should you be trying to created pseudo fixed column output if you are using Markdown?
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 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.