data have;
input col1 col2 col3 col4 col5 col99;
cards;
1 2 3 4 5 21
4 1 5 3 1 20
1 5 3 2 4 22
1 4 2 3 1 15
run;
data have;
set have;
if ~missing(col1) and ~missing(col99) then col11=put(col1,3.)||' ('||put((col1/col99)*100,5.1)||')';
if ~missing(col2) and ~missing(col99) then col21=put(col2,3.)||' ('||put((col2/col99)*100,5.1)||')';
if ~missing(col3) and ~missing(col99) then col31=put(col3,3.)||' ('||put((col3/col99)*100,5.1)||')';
if ~missing(col4) and ~missing(col99) then col41=put(col4,3.)||' ('||put((col4/col99)*100,5.1)||')';
if ~missing(col5) and ~missing(col99) then col51=put(col5,3.)||' ('||put((col5/col99)*100,5.1)||')';
run;
Can someone try it arrays Please I'm written in data steps
Avoid using ~, not sure that even works.
data have; input col1 col2 col3 col4 col5 col99; cards; 1 2 3 4 5 21 4 1 5 3 1 20 1 5 3 2 4 22 1 4 2 3 1 15 run; data want; set have; array v{6} col1--col99; array res{6} $50; do i=1 to 5; if not missing(v{i}) and not missing(v{6}) then res{i}=cat(put(v{i},3.),' (',put((v{i}/v{6})*100,5.1),')'); end; run;
Regardless of the final approach, I would suggest cutting down the number of comparisons. Using your original code:
if not missing(col99) then do;
if ~missing(col1) then col11=put(col1,3.)||' ('||put((col1/col99)*100,5.1)||')';
if ~missing(col2) then col21=put(col2,3.)||' ('||put((col2/col99)*100,5.1)||')';
if ~missing(col3) then col31=put(col3,3.)||' ('||put((col3/col99)*100,5.1)||')';
if ~missing(col4) then col41=put(col4,3.)||' ('||put((col4/col99)*100,5.1)||')';
if ~missing(col5) then col51=put(col5,3.)||' ('||put((col5/col99)*100,5.1)||')';
end;
That suggestion could easily be applied to an array-based solution, such as:
if not missing(col99) then do j=1 to 5;
Try not to overwrite your input.
You need two arrays one for the input counts and another for the generated strings.
data want ;
set have;
array col(5);
array result(5) $20 ;
if not missing(col99) then do i=1 to 5 ;
if missing(col(i)) then result(i)=' ';
else result(i)=put(col(i),3.)||' ('||put((col(i)/col99)*100,5.1)||')';
end;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.