<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Create Variables Representing Combinations of Others Based on Values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880179#M347777</link>
    <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;The solution you posted using transpose works great for something else i have been doing.&amp;nbsp; So many thanks.&amp;nbsp; Apologies for not making my example more clear from the start. I'll expand:&lt;/P&gt;&lt;P&gt;Each record/row represents a coalition in a community engaged in drug use prevention. The data represent their indication of whether they are currently engaged in efforts around that substance, where 1 = Yes and 0 = No. I have to report out the frequency/prevalence of each combination of substances that coalitions are engaged in.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;&lt;BR /&gt;input row meth rx nonrx;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 0 0&lt;BR /&gt;2 1 1 0&lt;BR /&gt;3 0 1 0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc print; run;&lt;/PRE&gt;&lt;P&gt;So, the first coalition is working on meth only, the second on meth and prescription drugs, and the third on prescription drugs only.&amp;nbsp; Note that nobody is working on non-rx (non-prescription opioids), among other combinations which i also need to report out (meaning i need to know that no coalitions are working on those combinations).&amp;nbsp; So manually, i was accomplish creating the indicators this way:&lt;/P&gt;&lt;PRE&gt;data want;
	set have;
	meth=0; meth_rx=0; meth_nonrx=0; meth_rx_nonrx=0; rx=0; rx_nonrx=0; nonrx=0;
	if meth = 1 and rx = 0 and nonrx = 0 then meth = 1;
	if meth = 1 and rx = 1 and nonrx = 0 then meth_rx = 1;
	if meth = 1 and rx = 0 and nonrx = 1 then meth_nonrx = 1;
	if meth = 1 and rx = 1 and nonrx = 1 then meth_rx_nonrx = 1;
	if meth = 0 and rx = 1 and nonrx = 0 then rx = 1;
	if meth = 0 and rx = 1 and nonrx = 1 then rx_nonrx = 1;
	if meth = 0 and rx = 0 and nonrx = 1 then nonrx = 1;
	run;
proc print; run;&lt;/PRE&gt;&lt;PRE&gt;                                                        meth_   meth_rx_
  Obs   row   meth_n   rx_n   nonrx_n   meth   meth_rx   nonrx     nonrx    rx   rx_nonrx   nonrx

   1     1       1       0       0        0       0        0         0       0       0        0
   2     2       1       1       0        0       0        0         0       0       0        0
   3     3       0       1       0        0       0        0         0       0       0        0&lt;/PRE&gt;&lt;P&gt;Currently I have two more substances to incorporate and (sadly) we add more substances from time to time.&amp;nbsp; Right now, i can create the variables and remain under 32. It seems like i should be able to use two arrays where the 2nd is one variable ahead...but i just can't wrap my head around the creation of the variable name.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Jun 2023 11:44:39 GMT</pubDate>
    <dc:creator>jasberger</dc:creator>
    <dc:date>2023-06-12T11:44:39Z</dc:date>
    <item>
      <title>Create Variables Representing Combinations of Others Based on Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880106#M347734</link>
      <description>&lt;P&gt;I have a set of indicators (1 or 0) for which i need to first identify all combinations and then create new variables indicating whether that combination is represented by each record (where representation is determined by 1 [versus 0]).&amp;nbsp; Here's a small example using three variables.&lt;/P&gt;&lt;PRE&gt;data have;
	input meth_n rx_n nonrx_n;
	cards;
	1 0 0
	1 1 0
	0 1 0
	run;&lt;/PRE&gt;&lt;P&gt;What i want:&lt;/P&gt;&lt;PRE&gt;data want;
	set have;
	if meth_n = 1 and rx_n = 0 and nonrx_n = 0 then meth = 1;
	if meth_n = 1 and rx_n = 1 and nonrx_n = 0 then meth_rx = 1;
	if meth_n = 0 and rx_n = 1 and nonrx_n = 0 then rx = 1;
	if meth_n = 0 and rx_n = 1 and nonrx_n = 1 then rx_nonrx = 1;
	run;&lt;/PRE&gt;&lt;P&gt;Note that i need to represent all combinations with new variables, even if that combination happens to not be present in the data itself. Here is an attempt i made where i can get a single field to represent the variables that have a '1', but i need variables created with those names.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data try; set have; length combo_var $200;
	array sub(*) meth_n rx_n nonrx_n;
	do i=1 to dim(sub);
		if sub(i) = 1 then do;
      		combo_var = catx("_", combo_var, vname(sub(i)));
	end; end;
	run;&lt;/PRE&gt;&lt;P&gt;Thanks for any help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Jun 2023 23:08:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880106#M347734</guid>
      <dc:creator>jasberger</dc:creator>
      <dc:date>2023-06-11T23:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create Variables Representing Combinations of Others Based on Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880112#M347740</link>
      <description>&lt;P&gt;Are you asking how to do this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do meth_n=0,1;
do rx_n=0,1;
do nnonrx_n=0,1;
  output;
end;
end;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jun 2023 00:16:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880112#M347740</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-12T00:16:16Z</dc:date>
    </item>
    <item>
      <title>Re: Create Variables Representing Combinations of Others Based on Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880114#M347742</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;No, i'm saying that if i have this data in hand:&lt;/P&gt;&lt;PRE&gt;data have;
	input meth_n rx_n nonrx_n;
	cards;
	1 0 0
	1 1 0
	0 1 0
	run;&lt;/PRE&gt;&lt;P&gt;I want it to look like 'want' from this:&lt;/P&gt;&lt;PRE&gt;data want;
	set have;
	if meth_n = 1 and rx_n = 0 and nonrx_n = 0 then meth = 1;
	if meth_n = 1 and rx_n = 1 and nonrx_n = 0 then meth_rx = 1;
	if meth_n = 0 and rx_n = 1 and nonrx_n = 0 then rx = 1;
	if meth_n = 0 and rx_n = 1 and nonrx_n = 1 then rx_nonrx = 1;
	run;&lt;/PRE&gt;&lt;P&gt;But, this is a smaller example...i have more variables to consider...and it seems like there should be a more automated way to get at this&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 00:32:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880114#M347742</guid>
      <dc:creator>jasberger</dc:creator>
      <dc:date>2023-06-12T00:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create Variables Representing Combinations of Others Based on Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880119#M347745</link>
      <description>&lt;P&gt;If you have more variables then you need to spell out the RULE for generating the output you want.&lt;/P&gt;
&lt;P&gt;It is not obvious.&lt;/P&gt;
&lt;P&gt;If the rule is to combine the variable names to make new variable names you will very soon make a string that is too long to use as a variable name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just want to make a VARIABLE with the concatenated names then that is pretty simple.&lt;/P&gt;
&lt;P&gt;You seem to be using only PART of the original variable names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input meth_n rx_n nonrx_n;
cards;
1 0 0
1 1 0
0 1 0
;

data want;
  set have;
  array nvar _numeric_;
  length combo $200 ;
  do index=1 to dim(nvar);
    if nvar[index] then combo=catx('_',combo,scan(vname(nvar[index]),1,'_'));
  end;
  drop index;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;OBS    meth_n    rx_n    nonrx_n    combo

 1        1        0        0       meth
 2        1        1        0       meth_rx
 3        0        1        0       rx


&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 02:18:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880119#M347745</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-12T02:18:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create Variables Representing Combinations of Others Based on Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880120#M347746</link>
      <description>&lt;P&gt;IF (and it is a big if when you have a lot of variables) the COMBO values are not too large to use as a variable name you can probably just use PROC TRANSPOSE to help you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fix_have;
  row+1;
  set have;
run;

proc transpose data=fix_have out=step1;
  by row;
run;

data step2;
  set step1;
  by row;
  where col1=1 ;
  length combo $200 ;
  retain combo ;
  if first.row then combo=' ';
  combo=catx('_',combo,scan(_name_,1,'_'));
  if last.row;
run;

proc transpose data=step2 out=want(drop=_name_);
  by row;
  id combo;
  var col1;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;OBS    row    meth    meth_rx    rx

 1      1       1        .        .
 2      2       .        1        .
 3      3       .        .        1
&lt;/PRE&gt;
&lt;P&gt;You can use PROC STDIZE to convert the missing values into zeros.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 02:41:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880120#M347746</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-12T02:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: Create Variables Representing Combinations of Others Based on Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880179#M347777</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;The solution you posted using transpose works great for something else i have been doing.&amp;nbsp; So many thanks.&amp;nbsp; Apologies for not making my example more clear from the start. I'll expand:&lt;/P&gt;&lt;P&gt;Each record/row represents a coalition in a community engaged in drug use prevention. The data represent their indication of whether they are currently engaged in efforts around that substance, where 1 = Yes and 0 = No. I have to report out the frequency/prevalence of each combination of substances that coalitions are engaged in.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;&lt;BR /&gt;input row meth rx nonrx;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 0 0&lt;BR /&gt;2 1 1 0&lt;BR /&gt;3 0 1 0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc print; run;&lt;/PRE&gt;&lt;P&gt;So, the first coalition is working on meth only, the second on meth and prescription drugs, and the third on prescription drugs only.&amp;nbsp; Note that nobody is working on non-rx (non-prescription opioids), among other combinations which i also need to report out (meaning i need to know that no coalitions are working on those combinations).&amp;nbsp; So manually, i was accomplish creating the indicators this way:&lt;/P&gt;&lt;PRE&gt;data want;
	set have;
	meth=0; meth_rx=0; meth_nonrx=0; meth_rx_nonrx=0; rx=0; rx_nonrx=0; nonrx=0;
	if meth = 1 and rx = 0 and nonrx = 0 then meth = 1;
	if meth = 1 and rx = 1 and nonrx = 0 then meth_rx = 1;
	if meth = 1 and rx = 0 and nonrx = 1 then meth_nonrx = 1;
	if meth = 1 and rx = 1 and nonrx = 1 then meth_rx_nonrx = 1;
	if meth = 0 and rx = 1 and nonrx = 0 then rx = 1;
	if meth = 0 and rx = 1 and nonrx = 1 then rx_nonrx = 1;
	if meth = 0 and rx = 0 and nonrx = 1 then nonrx = 1;
	run;
proc print; run;&lt;/PRE&gt;&lt;PRE&gt;                                                        meth_   meth_rx_
  Obs   row   meth_n   rx_n   nonrx_n   meth   meth_rx   nonrx     nonrx    rx   rx_nonrx   nonrx

   1     1       1       0       0        0       0        0         0       0       0        0
   2     2       1       1       0        0       0        0         0       0       0        0
   3     3       0       1       0        0       0        0         0       0       0        0&lt;/PRE&gt;&lt;P&gt;Currently I have two more substances to incorporate and (sadly) we add more substances from time to time.&amp;nbsp; Right now, i can create the variables and remain under 32. It seems like i should be able to use two arrays where the 2nd is one variable ahead...but i just can't wrap my head around the creation of the variable name.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 11:44:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880179#M347777</guid>
      <dc:creator>jasberger</dc:creator>
      <dc:date>2023-06-12T11:44:39Z</dc:date>
    </item>
    <item>
      <title>Re: Create Variables Representing Combinations of Others Based on Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880213#M347791</link>
      <description>&lt;P&gt;Are you trying to make flag variables that indicate, for example, that subject has METH and RX and no others?&lt;/P&gt;
&lt;P&gt;If so a simplified SAS code that takes advantage of the binary nature of the variables would look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;meth_rx_nonrx=meth and rx and nonrx ;
meth_rx=meth and rx and ^nonrx ;
meth_nonrx=meth and ^rx and nonrx ;
rx_nonrx=^meth and rx and nonrx ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if the goal is the make a flag that indicates the METH and RX combination, regardless of others then you would want something like this instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;meth_rx_nonrx=meth and rx and nonrx ;
meth_rx=meth and rx ;
meth_nonrx=meth and nonrx ;
rx_nonrx=rx and nonrx ;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jun 2023 14:17:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880213#M347791</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-12T14:17:22Z</dc:date>
    </item>
    <item>
      <title>Re: Create Variables Representing Combinations of Others Based on Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880215#M347792</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;OK, this is effectively what i have on my end.&amp;nbsp; I thought maybe there was a more data-driven/automated way to identify the combinations without having to type out the various yes-no setups to get there.&amp;nbsp; I'm going to mark your latest as the accepted solution. Thanks for the guidance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jason&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 14:40:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880215#M347792</guid>
      <dc:creator>jasberger</dc:creator>
      <dc:date>2023-06-12T14:40:07Z</dc:date>
    </item>
    <item>
      <title>Re: Create Variables Representing Combinations of Others Based on Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880222#M347794</link>
      <description>&lt;P&gt;So use the ALLCOMB function to get all combinations of 2 or more variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let names='meth' 'rx' 'nonrx';
%let n=%sysfunc(countw(&amp;amp;names,%str( )));

data combo ;
  array name[&amp;amp;n] $8 (&amp;amp;names);
  n=dim(name);
  do k=2 to n;
    ncomb=comb(n, k);
    do j=1 to ncomb;
      rc=allcomb(j, k, of name[*]);
      output;
    end;
  end;
  drop rc j;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;OBS    name1    name2    name3    n    k    ncomb

 1     meth     rx       nonrx    3    2      3
 2     meth     nonrx    rx       3    2      3
 3     rx       nonrx    meth     3    2      3
 4     rx       nonrx    meth     3    3      1
&lt;/PRE&gt;
&lt;P&gt;Then use that to generate your assignment statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code;
  set combo ;
  array name name: ;
  do  i=1 to k ;
    put name[i] @;
    if i &amp;lt; k then put +(-1) '_' @;
  end;
  put '= ' @ ;
  do i=1 to n ;
    if i&amp;gt;k then put '^' @ ;
    put name[i] @ ;
    if i &amp;lt; n then put 'and ' @;
  end;
  put ';' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;meth_rx = meth and rx and ^nonrx ;
meth_nonrx = meth and nonrx and ^rx ;
rx_nonrx = rx and nonrx and ^meth ;
rx_nonrx_meth = rx and nonrx and meth ;
&lt;/PRE&gt;
&lt;P&gt;And you can then copy and paste it or use %INCLUDE to add to a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 15:17:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Variables-Representing-Combinations-of-Others-Based-on/m-p/880222#M347794</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-12T15:17:57Z</dc:date>
    </item>
  </channel>
</rss>

