<?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 new variable with names from parts or all of an existing variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899462#M355522</link>
    <description>&lt;P&gt;others can try&amp;nbsp; trt_fl{i} = not missing(trt_reg{i}); in your code as well. That should work&lt;/P&gt;</description>
    <pubDate>Fri, 20 Oct 2023 20:27:40 GMT</pubDate>
    <dc:creator>crunchit</dc:creator>
    <dc:date>2023-10-20T20:27:40Z</dc:date>
    <item>
      <title>create new variable with names from parts or all of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899198#M355418</link>
      <description>&lt;P&gt;Hi folks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This might seem nuanced but is it possible to create new variables using parts of or all of an existing variable name in an array kind of style? If I understand correctly you can't change the format of an existing variable from say numeric to character or vice versa. In summary, I had to transpose a character variable that had a lot of distinct values (100+) so I could flag the occurrence and eventually count/sum the frequency. Is there an efficient way of creating another version of the existing 100+ new variables to be flagged and as numeric? I have tried this approach which seems to create the flags and overwrite the existing variable (not creating the supposedly new variable) but the format remains unchanged and it's still a character variable (not surprised as stated earlier that you can't change the format of an existing variable).&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data transp_trt_flags;
  set test.transp_bc_trt;
  /* Flag transposed treatment regimen */
  array trt_reg[*] trt_:; /*select all variables beginning with trt_*/
  array trt_fl[*]  trt_:_fl; /*create new dummy variables to replace the original; gave this a shot without thinking it will run*/ 

  do i = 1 to dim(trt_reg);
    if not missing(trt_reg(i)) then 
      trt_fl(i) = input(1, best32.);
    else
      trt_fl(i) = input(0, best32.);
  end;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="snippet_trt.png" style="width: 582px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88948i3AE8D1DBB70BF2F6/image-dimensions/582x134?v=v2" width="582" height="134" role="button" title="snippet_trt.png" alt="snippet_trt.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help as always&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 06:27:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899198#M355418</guid>
      <dc:creator>crunchit</dc:creator>
      <dc:date>2023-10-19T06:27:15Z</dc:date>
    </item>
    <item>
      <title>Re: create new variable with names from parts or all of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899202#M355420</link>
      <description>&lt;P&gt;Without giving it much thought - maybe you could create variable definitions as macro variables prior to your data step. Something like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   select dictinct trt into: trt_vars separated by ' '
   from have;
quit;

data want;
    set have;
    length &amp;amp;trt_vars 8.;
....
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 08:31:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899202#M355420</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-10-19T08:31:46Z</dc:date>
    </item>
    <item>
      <title>Re: create new variable with names from parts or all of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899203#M355421</link>
      <description>&lt;P&gt;Get the variable names from DICTIONARY.COLUMNS:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select cats(name,"_fl") into :new_array separated by " "
from dictionary.columns
where
  libname = "TEST" and memname = "TRANSP_BC_TRT"
  and upcase(name) like 'TRT_%'
;
quit;

data transp_trt_flags;
set test.transp_bc_trt;
array trt_reg{*} trt_:;
array trt_fl{*} &amp;amp;new_arr.;
do i = 1 to dim(trt_reg);
  trt_fl{i} = missing(trt_reg{i});
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can also see where your code can be greatly simplified.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 08:44:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899203#M355421</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-19T08:44:30Z</dc:date>
    </item>
    <item>
      <title>Re: create new variable with names from parts or all of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899237#M355438</link>
      <description>&lt;P&gt;If your transposed variable (in the VAR statement) is character type, then you might think of changing its format into numeric before transposing;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;like.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	num_var= put(char_var, best.);
run; 

proc transpose data= want out=want_wide;
	by &amp;lt;byvar&amp;gt;;
	var num_var;
	id &amp;lt;idvar&amp;gt;;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Oct 2023 13:30:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899237#M355438</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-10-19T13:30:06Z</dc:date>
    </item>
    <item>
      <title>Re: create new variable with names from parts or all of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899238#M355439</link>
      <description>&lt;P&gt;That seems logical&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;I will try it and definitely let you know. I appreciate it&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 13:53:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899238#M355439</guid>
      <dc:creator>crunchit</dc:creator>
      <dc:date>2023-10-19T13:53:16Z</dc:date>
    </item>
    <item>
      <title>Re: create new variable with names from parts or all of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899239#M355440</link>
      <description>&lt;P&gt;Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; I will give it a shot. I appreciate your help. Will keep you posted&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 13:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899239#M355440</guid>
      <dc:creator>crunchit</dc:creator>
      <dc:date>2023-10-19T13:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: create new variable with names from parts or all of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899241#M355441</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/321371"&gt;@A_Kh&lt;/a&gt;&amp;nbsp;Yeah,&amp;nbsp;I thought about that but did not try it mainly because I didn't think you could necessarily format a character variable say "boy" or "girl" into numeric. I may be wrong. Will try that as well. This is really piquing my interest now. Could have saved me a lot of time if I had just given it a shot when I thought about it. Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 14:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899241#M355441</guid>
      <dc:creator>crunchit</dc:creator>
      <dc:date>2023-10-19T14:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: create new variable with names from parts or all of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899460#M355521</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;thanks for your help. The first bit worked. Just a note for others to correct the typo in the macro variable plus I'm not quite sure of the logic for the second bit since it is returning a bunch of 1s for missing (need it to be the other way around for 1s if not missing) so I used my original code. I lost a few variables too. But all in all, considering the grand scheme of things, this will suffice. I appreciate it&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;&amp;amp;new_arr. should be &amp;amp;new_array.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 20:15:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899460#M355521</guid>
      <dc:creator>crunchit</dc:creator>
      <dc:date>2023-10-20T20:15:46Z</dc:date>
    </item>
    <item>
      <title>Re: create new variable with names from parts or all of an existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899462#M355522</link>
      <description>&lt;P&gt;others can try&amp;nbsp; trt_fl{i} = not missing(trt_reg{i}); in your code as well. That should work&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 20:27:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-new-variable-with-names-from-parts-or-all-of-an-existing/m-p/899462#M355522</guid>
      <dc:creator>crunchit</dc:creator>
      <dc:date>2023-10-20T20:27:40Z</dc:date>
    </item>
  </channel>
</rss>

