Dear Forum,
I would like to align the character columns containing the value 'frequency + (percentage)'
At the moment by following the attached paper I can align the data if the numbers of columns are known by creating an array.
https://www.pharmasug.org/proceedings/2013/CC/PharmaSUG-2013-CC15.pdf
Would it be any other way if the number of columns is unknown?
Thank you!
I don't see an array being used in the link referenced.
Can you make your need more explicit and provide example data and output?
Hi,
The array was done by myself by modifying/following the code in the paper and its working fine if the number of columns is known but wanted to know how to do if the number of columns is unknown or using in a macro code for a different number of columns.
data test(keep=olen: all);
set &indata;
all=1;
array len len1 len2;
array old col1 col2;
do i =1 to 2 by 1;
olen(i)=length(scan(col(i),1,'(','m'));
end;
run;
data test1;
merge &indata test;
by all;
run;
data test2;
set test1;
length newcol1 newcol2 $20;
array newcol newcol1 newcol2 ;
array vr vr1 vr2 ;
array len olen1 len2 ;
array pos pos1 pos2 ;
array dif dif1 dif2;
array old col1 col2;
do i =1 to 2 by 1;
vr(i)=length(scan(old(i),1,'(','m'));
dif(i)=olen(i)-vr(i);
if dif(i)=0 then newcol(i)=old(i);
if dif(i) ne 0 then do;
testtest=strip(put(dif(i), space.))||strip(old(i));
newcol(i)=tranwrd(testtest, 'a', ' '); end;
end;
run;
@Sami1234 wrote:
Hi,
The array was done by myself by modifying/following the code in the paper and its working fine if the number of columns is known but wanted to know how to do if the number of columns is unknown or using in a macro code for a different number of columns.
data test(keep=olen: all);
set &indata;
all=1;
array len len1 len2;
array old col1 col2;
do i =1 to 2 by 1;
olen(i)=length(scan(col(i),1,'(','m'));
end;
run;
data test1;
merge &indata test;
by all;
run;
data test2;
set test1;
length newcol1 newcol2 $20;
array newcol newcol1 newcol2 ;
array vr vr1 vr2 ;
array len olen1 len2 ;
array pos pos1 pos2 ;
array dif dif1 dif2;
array old col1 col2;
do i =1 to 2 by 1;
vr(i)=length(scan(old(i),1,'(','m'));
dif(i)=olen(i)-vr(i);
if dif(i)=0 then newcol(i)=old(i);
if dif(i) ne 0 then do;
testtest=strip(put(dif(i), space.))||strip(old(i));
newcol(i)=tranwrd(testtest, 'a', ' '); end;
end;
run;
Provide some actual data.
Placing two values in a single variable adds so much complexity.
What ODS destination are you sending this to?
I strongly suspect that there are number of ways to do this with style overrides and no need to do anything with column position counting or "fixing".
It's just an example for two, but sometimes the variables are around 15 and after that, they are going into the proc report.
@Sami1234 wrote:
It's just an example for two, but sometimes the variables are around 15 and after that, they are going into the proc report.
I repeat: Example data. I am not going to try to create a data set that MIGHT work with your example code to try to see what you are attempting.
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
In the past 6 years since that paper was written there have been lots of additions to style and table options for almost all of the ODS destinations (listing being the likely exception). The paper example looks a lot like something written using techniques from 30 years ago to force (monospace) line printer output to vertically align. There are many better options for most cases now days. But it usually helps to know which ODS Destination you are sending things to as each file type, HTML, PDF, RTF or (shudders) Excel have their own options that control output appearance.
Thanks for the reply.
The ODS Destination for the output will be the PDF
Thanks for the reply.
The ODS Destination for the output will be the PDF.
Is there any other way to align the data for the unknown number of columns which contain both stats and categorical (with percents) values?
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.