DATA Step, Macro, Functions and more

CREATE VECTOR

Reply
Contributor
Posts: 59

CREATE VECTOR

Hello, i want create a vector of all options from vector of strings : i have to "multiply" every cell to other cell

example :

i have a variable of characters

a

b

d

e


i want to create the next variable

ab

ad

ae

bd

be

de


how i can do it?

thank you


Super User
Super User
Posts: 6,502

Re: CREATE VECTOR

Normally in SAS you work an datasets not "vectors".  So if we create your input as a single variable dataset.

data have ;

length letter $1 ;

  input letter @@;

cards;

a b d e

;;;;

Then we can create your new single variable dataset from that using a DO loop and POINT= option on SET statement.

data want ;

  length next_variable $2 ;

  set have ;

  do p=_n_+1 to nobs ;

    set have (rename=(letter=next_letter)) nobs=nobs point=p ;

    next_variable = cats(letter,next_letter);

    output;

  end;

  keep next_variable ;

run;

Super User
Posts: 9,682

Re: CREATE VECTOR

This question seems to be for SQL.

Code: Program

data have ;
length letter $1 ;
  input letter @@;
cards;
a b d e
;;;;
run;
proc sql;
create table want as
  select cats(a.letter,b.letter) as new_letter
   from have as a,have as b
   where a.letter lt b.letter;
quit;

Xia Keshan

Message was edited by: xia keshan

Super User
Posts: 5,257

Re: CREATE VECTOR

Xie, agree thar SQL is a nice choice for this. But to comply withe requirement I think you need to change NE to GT.

Data never sleeps
Super User
Posts: 9,682

Re: CREATE VECTOR

Opps. Thanks Linus. Code has been updated. I think ARRAY maybe is a better choice .

Super User
Posts: 5,257

Re: CREATE VECTOR

If the actual data set is large, yes. But for normal data I think SQL is easier.

Data never sleeps
Super User
Posts: 9,682

Re: CREATE VECTOR

But if there is not order out there, SQL would NOT work , Like something:

b

a

c

-->

ba

bc

ac

消息编辑者为:xia keshan

Respected Advisor
Posts: 3,777

Re: CREATE VECTOR

Maybe you want combination.  Example straight out of SAS online documentation for LEXCOMB

data _null_;
  
array x[4] $3 ('a' 'b' 'd' 'e');
   n=dim(x);
   k=
2;
   ncomb=comb(n,k);
  
do j=1 to ncomb;
      rc=lexcomb(j, k, of x
  • );
         
  • put j 5. +3 x1-x2 +3 rc=;
          if rc<0 then leave;
          end;
      
    run;


       
    1   a b    rc=1
       
    2   a d    rc=2
       
    3   a e    rc=2
       
    4   b d    rc=1
       
    5   b e    rc=2
       
    6   d e    rc=1
    Contributor
    Posts: 59

    Re: CREATE VECTOR

    thank you, that what i need. can you please the meaning of of x

  • inside lexcomb? and what the meaning rc? thank you
  • Respected Advisor
    Posts: 3,777

    Re: CREATE VECTOR

    x

  • is a "SAS Variable List" meaning all variables in array x.  Lookup LEXCOMB for meaning of "Return Code" for this function.  Also lookup SAS Variable Lists.
  • Super User
    Super User
    Posts: 6,502

    Re: CREATE VECTOR

    Looks like the new software is not displaying this code properly. It has converted (*) into a bullet instead of asterisk inside of brackets.
    Ask a Question
    Discussion stats
    • 10 replies
    • 553 views
    • 0 likes
    • 5 in conversation