BookmarkSubscribeRSS Feed
AlexeyS
Pyrite | Level 9

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


10 REPLIES 10
Tom
Super User Tom
Super User

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;

Ksharp
Super User

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

LinusH
Tourmaline | Level 20

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
Ksharp
Super User

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

LinusH
Tourmaline | Level 20

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

Data never sleeps
Ksharp
Super User

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

b

a

c

-->

ba

bc

ac

消息编辑者为:xia keshan

data_null__
Jade | Level 19

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
    AlexeyS
    Pyrite | Level 9

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

  • inside lexcomb? and what the meaning rc? thank you
  • data_null__
    Jade | Level 19

    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.
  • Tom
    Super User Tom
    Super User
    Looks like the new software is not displaying this code properly. It has converted (*) into a bullet instead of asterisk inside of brackets.

    sas-innovate-2024.png

    Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

    Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

     

    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
    • 10 replies
    • 10580 views
    • 0 likes
    • 5 in conversation