BookmarkSubscribeRSS Feed
honeyhoney
Calcite | Level 5

Hi Guys,

I have several columns with column names like this:

A B C D E F G.....

what I want to do is to create new columns as A+B,A+C, A+D... and then B+C, B+D, B+E, ...  and C+D, C+E, C+F...

I need to repeat this process for many times. Anyone can give me a hint how to write macro on this?

Thank you.

3 REPLIES 3
data_null__
Jade | Level 19

I would use PROC SCORE

data score;
   retain _TYPE_ 'SCORE';
  
array _v
  • A B C D
  • /*E F G H I*/;
      
    length _name_ $32;
      
    do _k_ = 1 to dim(_v);
          do _i_ = _k_+1 to dim(_v);
             _name_ = catx('_',vname(_v[_k_]),vname(_v[_i_]));
             do _j_ = 1 to dim(_v);
                if _k_ eq _j_ or _i_ eq _j_ then _v[_j_] = 1;
               
    else _v[_j_] = 0;
               
    end;
            
    output;
            
    end;
         
    end;
      
    drop _k_ _i_ _j_;
       run;
    proc print;
      
    run;


    data test;
       array _v
  • A B C D
  • /*E F G H I*/ (1:4);
       do _n_ = 1 to 10;
         
    do _i_ = 1 to dim(_v);
             _v[_i_] = _v[_i_] * 2;
            
    end;
         
    output;
         
    end;
      
    drop _:;
       run;
    proc score data=test score=score out=scored;
       run;
    proc print;
      
    run;

    2-19-2015 4-37-20 PM.png  


    Missing works as well as zero so the SCORE data can be simplified slightly.
    data score;
       retain _TYPE_ 'SCORE';
      
    array _v
  • A B C D
  • /*E F G H I*/;
      
    length _name_ $32;
      
    do _k_ = 1 to dim(_v);
          _v[_k_] = 1;
         
    do _i_ = _k_+1 to dim(_v);
             _name_ = catx('_',vname(_v[_k_]),vname(_v[_i_]));
             _v[_i_] = 1;
            
    output;
            
    call missing(_v[_i_]);
             end;
         
    call missing(_v[_k_]);
          end;
      
    drop _k_ _i_;
       run;

    Message was edited by: data _null_

    Tom
    Super User Tom
    Super User

    You might need macro code (or at least some method to generate macro variables) to make up meaningful names, but to make the sums you can do with straight SAS code.

    %let names=a b c d e ;

    %let n=%sysfunc(countw(&names));

    data want;

    set have ;

    array single (&n) &names ;

    array double (&n,&n) ;

    do i=1 to &n ; do j=i+1 to &n ;

       double(&i,&j) = single(&i) + single(&j);

    end;

    run;

    honeyhoney
    Calcite | Level 5

    Thanks!

    sas-innovate-2024.png

    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.

     

    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.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

    Discussion stats
    • 3 replies
    • 1432 views
    • 0 likes
    • 3 in conversation