BookmarkSubscribeRSS Feed
rajeshalwayswel
Pyrite | Level 9

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

 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Astounding
PROC Star

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;

Tom
Super User Tom
Super User

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;
rajeshalwayswel
Pyrite | Level 9
Thanks Everyone for your support...

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1433 views
  • 0 likes
  • 4 in conversation