DATA Step, Macro, Functions and more

Can someone try it arrays?

Reply
Contributor
Posts: 52

Can someone try it arrays?

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

 

Super User
Super User
Posts: 7,988

Re: Can someone try it arrays?

Posted in reply to rajeshalwayswel

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;
Super User
Posts: 5,516

Re: Can someone try it arrays?

Posted in reply to rajeshalwayswel

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;

Super User
Super User
Posts: 7,076

Re: Can someone try it arrays?

[ Edited ]
Posted in reply to rajeshalwayswel

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;
Contributor
Posts: 52

Re: Can someone try it arrays?

Thanks Everyone for your support...
Ask a Question
Discussion stats
  • 4 replies
  • 91 views
  • 0 likes
  • 4 in conversation