<?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: Dynamic Field Creation and Naming in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755182#M238279</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Do you mean I need to create intermediary variables and then save them to the final variable.&amp;nbsp; I'm trying to save the result of the formula string to a data step variable.&amp;nbsp; The logic will create 200 variables for a wide table.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Jul 2021 01:54:56 GMT</pubDate>
    <dc:creator>DavidPhillips2</dc:creator>
    <dc:date>2021-07-20T01:54:56Z</dc:date>
    <item>
      <title>Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755113#M238227</link>
      <description>&lt;P&gt;Is there a way to automatically do the below;&lt;/P&gt;
&lt;TABLE width="709"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="417"&gt;if Year1 = 1 and academic_period = '200810' then Fall _2008 =1;&lt;/TD&gt;
&lt;TD width="98"&gt;Year1&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;200810&lt;/TD&gt;
&lt;TD width="66"&gt;Fall _2008&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year2 = 1 and academic_period = '200710' then Fall _2008 =1;&lt;/TD&gt;
&lt;TD&gt;Year2&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200710&lt;/TD&gt;
&lt;TD&gt;Fall _2008&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year3 = 1 and academic_period = '200610' then Fall _2008 =1;&lt;/TD&gt;
&lt;TD&gt;Year3&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200610&lt;/TD&gt;
&lt;TD&gt;Fall _2008&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year4 = 1 and academic_period = '200510' then Fall _2008 =1;&lt;/TD&gt;
&lt;TD&gt;Year4&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200510&lt;/TD&gt;
&lt;TD&gt;Fall _2008&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year5 = 1 and academic_period = '200410' then Fall _2008 =1;&lt;/TD&gt;
&lt;TD&gt;Year5&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200410&lt;/TD&gt;
&lt;TD&gt;Fall _2008&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year6 = 1 and academic_period = '200310' then Fall _2008 =1;&lt;/TD&gt;
&lt;TD&gt;Year6&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200310&lt;/TD&gt;
&lt;TD&gt;Fall _2008&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year1 = 1 and academic_period = '200910' then Fall_2009 =1;&lt;/TD&gt;
&lt;TD&gt;Year1&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200910&lt;/TD&gt;
&lt;TD&gt;Fall_2009&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year2 = 1 and academic_period = '200810' then Fall_2009 =1;&lt;/TD&gt;
&lt;TD&gt;Year2&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200810&lt;/TD&gt;
&lt;TD&gt;Fall_2009&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year3 = 1 and academic_period = '200710' then Fall_2009 =1;&lt;/TD&gt;
&lt;TD&gt;Year3&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200710&lt;/TD&gt;
&lt;TD&gt;Fall_2009&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year4 = 1 and academic_period = '200610' then Fall_2009 =1;&lt;/TD&gt;
&lt;TD&gt;Year4&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200610&lt;/TD&gt;
&lt;TD&gt;Fall_2009&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year5 = 1 and academic_period = '200510' then Fall_2009 =1;&lt;/TD&gt;
&lt;TD&gt;Year5&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200510&lt;/TD&gt;
&lt;TD&gt;Fall_2009&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year6 = 1 and academic_period = '200410' then Fall_2009 =1;&lt;/TD&gt;
&lt;TD&gt;Year6&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200410&lt;/TD&gt;
&lt;TD&gt;Fall_2009&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year1 = 1 and academic_period = '201010' then Fall_2010 =1;&lt;/TD&gt;
&lt;TD&gt;Year1&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;201010&lt;/TD&gt;
&lt;TD&gt;Fall_2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year2 = 1 and academic_period = '200910' then Fall_2010 =1;&lt;/TD&gt;
&lt;TD&gt;Year2&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200910&lt;/TD&gt;
&lt;TD&gt;Fall_2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year3 = 1 and academic_period = '200810' then Fall_2010 =1;&lt;/TD&gt;
&lt;TD&gt;Year3&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200810&lt;/TD&gt;
&lt;TD&gt;Fall_2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year4 = 1 and academic_period = '200710' then Fall_2010 =1;&lt;/TD&gt;
&lt;TD&gt;Year4&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200710&lt;/TD&gt;
&lt;TD&gt;Fall_2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year5 = 1 and academic_period = '200610' then Fall_2010 =1;&lt;/TD&gt;
&lt;TD&gt;Year5&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200610&lt;/TD&gt;
&lt;TD&gt;Fall_2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;if Year6 = 1 and academic_period = '200510' then Fall_2010 =1;&lt;/TD&gt;
&lt;TD&gt;Year6&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;200510&lt;/TD&gt;
&lt;TD&gt;Fall_2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;I'm trying to dynamically create fields based on a formula.&amp;nbsp; The probability of human error in the future is too high if I just use one SAS line per statement here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The part that I want is the dynamic&amp;nbsp; Fall_XXXX = 1 part based on a formula.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I experimented with&amp;nbsp;*catx(' ', 'Fall', sum(input(substr(academic_period, 1, 4), 4.), 0)) = 1;&amp;nbsp; But I'm missing the syntax to make this line work.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jul 2021 19:58:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755113#M238227</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2021-07-19T19:58:46Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755118#M238231</link>
      <description>&lt;P&gt;I am not sure exactly what your STARTING data looks like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am very surprised that "academic_period" values from what appear to be 6 calendar year like values all get assigned the same Fall_XXXX value. Are you sure there are no typos in there?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What was this supposed to do?&lt;/P&gt;
&lt;PRE&gt;sum(input(substr(academic_period, 1, 4), 4.), 0))&lt;/PRE&gt;
&lt;P&gt;You are summing some value with zero. So why bother?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It may help to tell why you have "year1" etc to begin with and what the underlying rules are. This is one case where a working example may not actually provide sufficient information for a general algorithm such as what to do "next year" when Year7=1 and some academic period.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that there may be a possible solution with Intnx, your academic period and something extracted from the Year value but I am not going to expend much effort trying to derive obscure rules from an example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe we have indicated a preference for starting data as data step code to give us something to work with before this.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jul 2021 20:15:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755118#M238231</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-19T20:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755130#M238235</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
	create table temp (time_variable varchar(50),
	acaedemic_period_base varchar(6),
	want varchar (50)
);
quit;
proc sql;insert into temp(time_variable, acaedemic_period_base, want) values ('Year1', '200810', 'Fall _2008 =1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year2', '200710', 'Fall _2008 =1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year3', '200610', 'Fall _2008 =1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year4', '200510', 'Fall _2008 =1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year5', '200410', 'Fall _2008 =1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year6', '200310', 'Fall _2008 =1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year1', '200910', 'Fall_2009 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year2', '200810', 'Fall_2009 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year3', '200710', 'Fall_2009 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year4', '200610', 'Fall_2009 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year5', '200510', 'Fall_2009 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year6', '200410', 'Fall_2009 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year1', '201010', 'Fall_2010 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year2', '200910', 'Fall_2010 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year3', '200810', 'Fall_2010 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year4', '200710', 'Fall_2010 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year5', '200610', 'Fall_2010 = 1');
insert into temp(time_variable, acaedemic_period_base, want) values ('Year6', '200510', 'Fall_2010 = 1');

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Jul 2021 20:48:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755130#M238235</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2021-07-19T20:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755131#M238236</link>
      <description>&lt;P&gt;The formula I pasted was to dynamically create fields like Fall 2010 and set the value equal to 1 based on a formula;&amp;nbsp; I'm trying to figure out how to use any formula to create a dynamically named field.&amp;nbsp; The fields will be used in a wide table.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jul 2021 20:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755131#M238236</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2021-07-19T20:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755138#M238243</link>
      <description>&lt;P&gt;You need to describe better what you are trying to do.&amp;nbsp; If you want to dynamically name a variable you have two choices.&amp;nbsp; Use PROC TABULATE with its ID statement to tell it which variable to use as the name.&amp;nbsp; Or use some code generation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But perhaps you just want to keep the data in a tall skinny structure instead of creating new variables.&amp;nbsp; You could always create a report that converts it to a more rectangular layoff for viewing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jul 2021 21:29:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755138#M238243</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-19T21:29:59Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755147#M238250</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; You are summing some value with zero. So why bother?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;This prevents returning missing values. Whether that's a good thing here remains to be seen.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jul 2021 22:24:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755147#M238250</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-19T22:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755149#M238251</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;I experimented with&amp;nbsp;*catx(' ', 'Fall', sum(input(substr(academic_period, 1, 4), 4.), 0)) = 1;&amp;nbsp; But I'm missing the syntax to make this line work.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This line will not work. Variable names are all defined when the data step is compiled. You can't have functions to the left of the = sign (exceptions apply).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your choices are:&lt;/P&gt;
&lt;P&gt;- A macro that writes the code creating the variables you want. This code is then compiled as part of the data step.&lt;/P&gt;
&lt;P&gt;- Creating variables that contain the values you need and then transposing the table to convert these values into variable names.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jul 2021 22:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755149#M238251</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-19T22:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755182#M238279</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Do you mean I need to create intermediary variables and then save them to the final variable.&amp;nbsp; I'm trying to save the result of the formula string to a data step variable.&amp;nbsp; The logic will create 200 variables for a wide table.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 01:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755182#M238279</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2021-07-20T01:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755183#M238280</link>
      <description>&lt;P&gt;I'm trying to dynamically create a wide table off of a formula that defines many variables.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 01:57:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755183#M238280</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2021-07-20T01:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755184#M238281</link>
      <description>&lt;P&gt;I am saying all the variable names must be known when the data step compiles, so before it runs. You can't add variables after compilation.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 01:57:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755184#M238281</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-20T01:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755193#M238283</link>
      <description>&lt;P&gt;Can I define them with a length statement and then populate them dynamically&amp;nbsp;like with the catx statement?&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 02:07:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755193#M238283</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2021-07-20T02:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755196#M238285</link>
      <description>&lt;P&gt;If you want to go this route (rather than transposing) then you can define the variables in an array statement, and then refer to either the array index (position) or the variable name associated with that index. Like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data t;
  array A[3] VAR1 VAR2 VAR3;
  A[1]=1;
  VAR2=2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 02:13:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755196#M238285</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-20T02:13:24Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755202#M238287</link>
      <description>&lt;P&gt;I'm trying to identify the name of the variable backward, the array method predefines the name.&amp;nbsp; Since I'm working backward it might make sense to transpose.&amp;nbsp; I've used transpose before, I'll look into options with it tomorrow.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 02:17:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755202#M238287</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2021-07-20T02:17:36Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755204#M238288</link>
      <description>&lt;P&gt;If you don't know the variable names in advance, then transposing is probably better. The other option would be to build the names in a macro and then use the macro to define the array. That's more convoluted.&lt;/P&gt;
&lt;P&gt;Having said that, if the names are random, how can you build a program that uses them?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 02:26:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755204#M238288</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-20T02:26:08Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755217#M238300</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5059"&gt;@DavidPhillips2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm trying to dynamically create a wide table off of a formula that defines many variables.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;PLEASE PROVIDE EXAMPLE DATA.&lt;/P&gt;
&lt;P&gt;Show what SAS code you want to create from that data.&lt;/P&gt;
&lt;P&gt;If you want to generate code from DATA the easiest way is to use the PUT statement in a data step to write the code to a file. You can then use %INCLUDE to execute the generated code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you haven't really provided any workable example data or code let's just make a test case.&lt;/P&gt;
&lt;P&gt;Say you have a dataset name META with two variables:&amp;nbsp; VAR and CODE.&amp;nbsp; The first has the name to use for a new variable and the second has the expression to use to assign a value to that new variable.&amp;nbsp; Then you can generate a series of assignment statement to create the new variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set meta;
   file print;
   put var '=(' code ');' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if have another dataset named HAVE that contains the actual data (the variables that are referenced by the CODE values) then you can use the generated code to create new variables by including it into a data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
%include code / source2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 04:28:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755217#M238300</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-20T04:28:16Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755356#M238366</link>
      <description>&lt;P&gt;The names are not random.&amp;nbsp; They are derived based on a formula.&amp;nbsp; The list will be Fall 2008 - Fall 2020 after being derived.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 13:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755356#M238366</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2021-07-20T13:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755357#M238367</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5059"&gt;@DavidPhillips2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The names are not random.&amp;nbsp; They are derived based on a formula.&amp;nbsp; The list will be Fall 2008 - Fall 2020 after being derived.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In that case do NOT use dynamic names.&amp;nbsp; Fix the names and use an array.&amp;nbsp; Then you can use a calcalated YEAR number to index into the array and decide which variable to use to store the value (or which value to read).&lt;/P&gt;
&lt;P&gt;So something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array fall [2008:2020] fall2008-fall2020;
  year = ** formula1 **;
  fall[year] = ** formula 2 ** ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Jul 2021 13:18:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755357#M238367</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-20T13:18:12Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755371#M238373</link>
      <description>&lt;P&gt;I follow the logic in general.&amp;nbsp; I'm trying to follow the details.&amp;nbsp; The idea is to populate the fields of the Fall 2008- Fall2020 after identifying which field the measure relates to.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
	create table temp (time_variable varchar(50),
	acaedemic_period_base varchar(6),
	want varchar (50),
	measure1 num(1)
);
quit;
proc sql;insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year1', '200810', 'Fall _2008 =1', 1);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year2', '200710', 'Fall _2008 =1', 1);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year3', '200610', 'Fall _2008 =1', 1);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year4', '200510', 'Fall _2008 =1', 1);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year5', '200410', 'Fall _2008 =1', 1);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year6', '200310', 'Fall _2008 =1', 0);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year1', '200910', 'Fall_2009 = 1', 1);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year2', '200810', 'Fall_2009 = 1', 1);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year3', '200710', 'Fall_2009 = 1', 1);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year4', '200610', 'Fall_2009 = 1', 0);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year5', '200510', 'Fall_2009 = 1', 0);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year6', '200410', 'Fall_2009 = 1', 0);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year1', '201010', 'Fall_2010 = 1', 1);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year2', '200910', 'Fall_2010 = 1', 0);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year3', '200810', 'Fall_2010 = 1', 0);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year4', '200710', 'Fall_2010 = 1', 0);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year5', '200610', 'Fall_2010 = 1', 0);
insert into temp(time_variable, acaedemic_period_base, want, measure1) values ('Year6', '200510', 'Fall_2010 = 1', 0);

quit;

data want;
  set temp;
  array fall [2008:2020] Fall_2008-Fall_2020;
  /*find the year referenced*/
  year = catx(' ', 'Fall', sum(input(substr(acaedemic_period_base, 1, 4), 4.), 0));
  /*record the 1 or 0 of the referenced year to the Fall_2008-Fall_2020 array slot*/
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Jul 2021 14:10:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755371#M238373</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2021-07-20T14:10:20Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755375#M238374</link>
      <description>&lt;P&gt;Thanks for posting data.&amp;nbsp; Note here is a much easier way to create such a dataset that is also easier to review as text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
  infile cards dsd truncover ;
  input time_variable :$50. acaedemic_period_base :$6. want :$50. measure1 ;
cards;
Year1,200810,Fall _2008 =1,1
Year2,200710,Fall _2008 =1,1
Year3,200610,Fall _2008 =1,1
Year4,200510,Fall _2008 =1,1
Year5,200410,Fall _2008 =1,1
Year6,200310,Fall _2008 =1,0
Year1,200910,Fall_2009 = 1,1
Year2,200810,Fall_2009 = 1,1
Year3,200710,Fall_2009 = 1,1
Year4,200610,Fall_2009 = 1,0
Year5,200510,Fall_2009 = 1,0
Year6,200410,Fall_2009 = 1,0
Year1,201010,Fall_2010 = 1,1
Year2,200910,Fall_2010 = 1,0
Year3,200810,Fall_2010 = 1,0
Year4,200710,Fall_2010 = 1,0
Year5,200610,Fall_2010 = 1,0
Year6,200510,Fall_2010 = 1,0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;So you can use SCAN() to pull out "words" from your WANT string and use INPUT() to convert those strings into numbers.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set temp;
  year = input(scan(want,-2,'_='),32.);
  array fall [2008:2010] Fall_2008-Fall_2010;
  if 2008 &amp;lt;= year &amp;lt;= 2010 then fall[year]=input(scan(want,-1,'='),32.);
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                   acaedemic_
        time_       period_                                           Fall_    Fall_    Fall_
Obs    variable       base           want         measure1    year     2008     2009     2010

  1     Year1        200810      Fall _2008 =1        1       2008      1        .        .
  2     Year2        200710      Fall _2008 =1        1       2008      1        .        .
  3     Year3        200610      Fall _2008 =1        1       2008      1        .        .
  4     Year4        200510      Fall _2008 =1        1       2008      1        .        .
  5     Year5        200410      Fall _2008 =1        1       2008      1        .        .
  6     Year6        200310      Fall _2008 =1        0       2008      1        .        .
  7     Year1        200910      Fall_2009 = 1        1       2009      .        1        .
  8     Year2        200810      Fall_2009 = 1        1       2009      .        1        .
  9     Year3        200710      Fall_2009 = 1        1       2009      .        1        .
 10     Year4        200610      Fall_2009 = 1        0       2009      .        1        .
 11     Year5        200510      Fall_2009 = 1        0       2009      .        1        .
 12     Year6        200410      Fall_2009 = 1        0       2009      .        1        .
 13     Year1        201010      Fall_2010 = 1        1       2010      .        .        1
 14     Year2        200910      Fall_2010 = 1        0       2010      .        .        1
 15     Year3        200810      Fall_2010 = 1        0       2010      .        .        1
 16     Year4        200710      Fall_2010 = 1        0       2010      .        .        1
 17     Year5        200610      Fall_2010 = 1        0       2010      .        .        1
 18     Year6        200510      Fall_2010 = 1        0       2010      .        .        1

&lt;/PRE&gt;
&lt;P&gt;But that seems really silly.&amp;nbsp; Don't you just want to pull the year from the academic variable instead?&lt;/P&gt;
&lt;P&gt;If so then how do you get from academic year of "200710" or "200610" to FALL_2010?&lt;/P&gt;
&lt;P&gt;Perhaps by adding the digit in the TIME variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set temp;
  base_year=input(acaedemic_period_base,4.);
  offset=input(substr(time_variable,5),32.);
  array fall [2008:2010] Fall_2008-Fall_2010;
  fall[base_year+offset-1]=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Jul 2021 14:38:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755375#M238374</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-20T14:38:41Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Field Creation and Naming</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755392#M238379</link>
      <description>&lt;P&gt;To make it truly dynamic use PROC TRANSPOSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data step1;
  set temp;
  calendar_year=input(acaedemic_period_base,4.)+input(substr(time_variable,5),32.)-1;
run;

proc transpose data=step1 prefix=FALL_ out=want(drop=_name_);
  by time_variable acaedemic_period_base notsorted;
  id calendar_year;
  var measure1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                   acaedemic_
        time_       period_      FALL_    FALL_    FALL_
Obs    variable       base        2008     2009     2010

  1     Year1        200810        1        .        .
  2     Year2        200710        1        .        .
  3     Year3        200610        1        .        .
  4     Year4        200510        1        .        .
  5     Year5        200410        1        .        .
  6     Year6        200310        0        .        .
  7     Year1        200910        .        1        .
  8     Year2        200810        .        1        .
  9     Year3        200710        .        1        .
 10     Year4        200610        .        0        .
 11     Year5        200510        .        0        .
 12     Year6        200410        .        0        .
 13     Year1        201010        .        .        1
 14     Year2        200910        .        .        0
 15     Year3        200810        .        .        0
 16     Year4        200710        .        .        0
 17     Year5        200610        .        .        0
 18     Year6        200510        .        .        0

&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Jul 2021 15:04:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Field-Creation-and-Naming/m-p/755392#M238379</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-20T15:04:53Z</dc:date>
    </item>
  </channel>
</rss>

