<?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: Dynamically create variables in the data step based on values of another variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118277#M293229</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Post sample data and sample output I think to help understand the problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think you could at least combine step 2/3 using a proc transpose step instead of macro variables, but would need to see more info to be sure. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 21 Aug 2013 19:18:26 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2013-08-21T19:18:26Z</dc:date>
    <item>
      <title>Dynamically create variables in the data step based on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118276#M293228</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I regularly have to process flags from comma-separated character fields that look like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My current process involves the following steps:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Data step that iterates through the comma-separated values and outputs each one into a new dataset&lt;/LI&gt;&lt;LI&gt;Proc freq of those values and store them into macro variables&lt;/LI&gt;&lt;LI&gt;A second data step that checks for the presence of the values and assigns them to a binary variable flag&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am wondering whether all of this can be reduced to a single datastep.&amp;nbsp; I have dozens of variable like this and I would like to create a macro that can dynamically create flags for each comma-separated value in one datastep.&amp;nbsp; Something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data tmp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %expand_list(example_field);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;/* yields the output:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flag_AX&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flag_EQ&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flag_XX&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flag_DL&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;*/&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is this wishful thinking?&amp;nbsp; I understand the timing issues between the data step and the macro compilation steps so I think I may be out of luck.&amp;nbsp; I'm hoping someone can give me a glimmer of hope, though.&amp;nbsp; Maybe something like proc FCMP?&amp;nbsp; The flag doesn't have to be strictly [1,0], it could also be [1, .]&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Aug 2013 19:16:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118276#M293228</guid>
      <dc:creator>Zelazny7</dc:creator>
      <dc:date>2013-08-21T19:16:17Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create variables in the data step based on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118277#M293229</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Post sample data and sample output I think to help understand the problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think you could at least combine step 2/3 using a proc transpose step instead of macro variables, but would need to see more info to be sure. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Aug 2013 19:18:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118277#M293229</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-08-21T19:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create variables in the data step based on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118278#M293230</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Have you looked at the SYMPUT and SYMGET routines to see if they can help you accomplish what you are after.&amp;nbsp; See here for documentation: &lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000210266.htm" title="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000210266.htm"&gt;SAS(R) 9.2 Macro Language: Reference&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Aug 2013 19:23:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118278#M293230</guid>
      <dc:creator>scottdmurff</dc:creator>
      <dc:date>2013-08-21T19:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create variables in the data step based on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118279#M293231</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, thanks, I've looked into those.&amp;nbsp; The problem I'm having is that the variables I want to create derive their names from the values of another field.&amp;nbsp; SYMGET will assign a macro variable value to a variable I have already created, but it won't create the variable for me dynamically.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Aug 2013 19:27:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118279#M293231</guid>
      <dc:creator>Zelazny7</dc:creator>
      <dc:date>2013-08-21T19:27:36Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create variables in the data step based on values of another variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118280#M293232</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Bad example data so hard to determine if this is correct or not:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; i=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; do while (scan(example_field, i, ",") ne "");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var_flip=scan(example_field, i, ",");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; i+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc transpose data=want1 out=want2 prefix=flag_;&lt;/P&gt;&lt;P&gt;id var_flip;&lt;/P&gt;&lt;P&gt;var value;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 21 Aug 2013 19:34:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-variables-in-the-data-step-based-on-values-of/m-p/118280#M293232</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-08-21T19:34:44Z</dc:date>
    </item>
  </channel>
</rss>

