<?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: Creating SAS Arrays Using Column Values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486890#M126756</link>
    <description>Hi Reeza, the date check variables are SAS dates. However, I created new variables 'DT_CHECK_STR' and 'HYST_DT_STR' that are strings that take values that resemble that column names (e.g., DT_CHECK_STR can be 'Y10_M6'). I was thinking I could do something with it.&lt;BR /&gt;&lt;BR /&gt;Could you elaborate by what you mean by changing the indices for the array? Thank you!&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Wed, 15 Aug 2018 03:28:27 GMT</pubDate>
    <dc:creator>bhong</dc:creator>
    <dc:date>2018-08-15T03:28:27Z</dc:date>
    <item>
      <title>Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486877#M126750</link>
      <description>&lt;P&gt;Hi, I'm not entirely sure if I can accurately describe what I want to do, but here's an attempt:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that's structured similar to this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Y11_M6    Y11_M7    Y11_M8    Y11_M9    Y11_M10    Y11_M11    Y11_M12    Y12_M1   Y12_M2    Y12_M3    DT_CHECK_STR    HYST_DT_STR
1         1         1         1         1          1          1          1        1         1         Y10_M6          Y12_M4&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are more columns before Y11_M6. I want to only add up the values for the variables that fall between DT_CHECK_STR and HYST_DT_STR. I wish to use arrays to solve this problem, but I'm having no success thus far (and don't know if it's even at all possible). Here's some dummy code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
	SET mem(OBS=1); 
	DT_CHECK = intnx('month', EVENT_DT, -10);
	DT_CHECK_STR = cats('Y', put(DT_CHECK, YEAR2.), '_', 'M', month(DT_CHECK));
	HYST_DT_STR = cats('Y', put(EVENT_DT, YEAR2.), '_', 'M', month(EVENT_DT));


	ARRAY vars[*] Y80_M1--Y12_M3;
	DO i=1 TO dim(vars);
		/* IF varname(vars[i]) "between" DT_CHECK_STR and HYST_DT_STR then sum_n = sum(of vars{i}); */ 
	END;

	FORMAT DT_CHECK mmddyy10.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The commented code is clearly wrong, but I wanted to write something as an "example."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another way I wanted to tackle the issue is to create arrays using macro variables. So I would imagine something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let x = DT_CHECK_STR;
%let y = HYST_DT_STR;
array vars[*] &amp;amp;x--&amp;amp;y;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is my coding problem at all feasible? Any help would be great. Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 15 Aug 2018 00:03:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486877#M126750</guid>
      <dc:creator>bhong</dc:creator>
      <dc:date>2018-08-15T00:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486885#M126754</link>
      <description>&lt;P&gt;What do the date check values look like?&lt;/P&gt;
&lt;P&gt;You can change the indexes for an array and that's usually a good method to use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/225184"&gt;@bhong&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I'm not entirely sure if I can accurately describe what I want to do, but here's an attempt:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset that's structured similar to this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Y11_M6    Y11_M7    Y11_M8    Y11_M9    Y11_M10    Y11_M11    Y11_M12    Y12_M1   Y12_M2    Y12_M3    DT_CHECK_STR    HYST_DT_STR
1         1         1         1         1          1          1          1        1         1         Y10_M6          Y12_M4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are more columns before Y11_M6. I want to only add up the values for the variables that fall between DT_CHECK_STR and HYST_DT_STR. I wish to use arrays to solve this problem, but I'm having no success thus far (and don't know if it's even at all possible). Here's some dummy code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
	SET mem(OBS=1); 
	DT_CHECK = intnx('month', EVENT_DT, -10);
	DT_CHECK_STR = cats('Y', put(DT_CHECK, YEAR2.), '_', 'M', month(DT_CHECK));
	HYST_DT_STR = cats('Y', put(EVENT_DT, YEAR2.), '_', 'M', month(EVENT_DT));


	ARRAY vars[*] Y80_M1--Y12_M3;
	DO i=1 TO dim(vars);
		/* IF varname(vars[i]) "between" DT_CHECK_STR and HYST_DT_STR then sum_n = sum(of vars{i}); */ 
	END;

	FORMAT DT_CHECK mmddyy10.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The commented code is clearly wrong, but I wanted to write something as an "example."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another way I wanted to tackle the issue is to create arrays using macro variables. So I would imagine something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let x = DT_CHECK_STR;
%let y = HYST_DT_STR;
array vars[*] &amp;amp;x--&amp;amp;y;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is my coding problem at all feasible? Any help would be great. Thank you!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Aug 2018 02:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486885#M126754</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-15T02:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486890#M126756</link>
      <description>Hi Reeza, the date check variables are SAS dates. However, I created new variables 'DT_CHECK_STR' and 'HYST_DT_STR' that are strings that take values that resemble that column names (e.g., DT_CHECK_STR can be 'Y10_M6'). I was thinking I could do something with it.&lt;BR /&gt;&lt;BR /&gt;Could you elaborate by what you mean by changing the indices for the array? Thank you!&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 15 Aug 2018 03:28:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486890#M126756</guid>
      <dc:creator>bhong</dc:creator>
      <dc:date>2018-08-15T03:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486891#M126757</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demo;

array myData(2014:2018) Y2014 Y2015 Y2016 Y2017 Y2018 (1 0 1 0 1);

do year=2014 to 2018; *can use hbound/lbound to get these values;
myData(i) = myData(i) * 20;
end;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could probably set up a two dimensional array, years across and months and then use those as your index within a double loop....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stats.idre.ucla.edu/wp-content/uploads/2016/02/bt3009.pdf" target="_blank"&gt;https://stats.idre.ucla.edu/wp-content/uploads/2016/02/bt3009.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Aug 2018 03:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486891#M126757</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-15T03:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486893#M126758</link>
      <description>&lt;P&gt;If there will always be corresponding variables for both&amp;nbsp;DT_CHECK_STR and HYST_DT_STR, and the variables you want to sum are in date order, you could use something as simple as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mem;
  input Y11_M6-Y11_M12 Y12_M1-Y12_M4 (DT_CHECK_STR HYST_DT_STR) ($);
  cards;
1 1 1 1 1 1 1 1 1 1 1 Y11_M9 Y12_M4
;

data _null_;
  set mem (obs=1);
  call execute('data want;set mem;');
  forexec=catx(' ','total=sum(of',vvalue(DT_CHECK_STR),'--',vvalue(HYST_DT_STR),');run;');
  call execute(forexec);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Aug 2018 03:53:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486893#M126758</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-08-15T03:53:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486896#M126760</link>
      <description>&lt;P&gt;Hi Reeza,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to do things a different way.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test2;
	SET test; 
	DT_CHECK = intnx('month', EVENT_DT, -10);
	DT_CHECK_STR = cats('Y', put(DT_CHECK, YEAR2.), '_', 'M', month(DT_CHECK));
	HYST_DT_STR = cats('Y', put(EVENT_DT, YEAR2.), '_', 'M', month(EVENT_DT));
	j=0;
	array vars[*] Y11_M6--Y12_M3;
	do i=1 to dim(vars);
		YEAR = substr(scan(vname(vars[i]), 1, "_"), 2, 2);
		MONTH = input(substr(scan(vname(vars[i]), 2, "_"), 2), 2.);
	
		IF substr(YEAR, 1, 1) = '8' OR substr(YEAR, 1, 1) = '9' THEN NEW_YEAR = input(cats('19', YEAR), 4.);
			ELSE NEW_YEAR = input(cats('20', YEAR), 4.);
		
		IF DT_CHECK &amp;lt;= mdy(MONTH, 1, NEW_YEAR) &amp;lt;= EVENT_DT THEN /* don't know what to include here */;
	end;
	
	
	FORMAT DT_CHECK mmddyy10.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I converted all the column names into SAS dates and used an IF statement to see if those "new" dates are between DT_CHECK and EVENT_DT. However, I am stuck--perhaps you could tell me if my method makes sense? Ultimately, if the new date formed using the column name is between DT_CHECK and EVENT_DT, I only want to sum up the columns in the range. Could you look at my code above and point me in the right direction?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Aug 2018 04:13:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486896#M126760</guid>
      <dc:creator>bhong</dc:creator>
      <dc:date>2018-08-15T04:13:10Z</dc:date>
    </item>
    <item>
      <title>Re: Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486899#M126761</link>
      <description>&lt;P&gt;Assuming your dates check out, it would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;then sum_total = sum(sum_total, vars(i));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Aug 2018 04:19:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486899#M126761</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-15T04:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486903#M126764</link>
      <description>&lt;P&gt;This seems to work on my test data. Thank you so much. I'll try it once I'm in the office tomorrow--excited to go to work! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Aug 2018 04:25:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486903#M126764</guid>
      <dc:creator>bhong</dc:creator>
      <dc:date>2018-08-15T04:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486904#M126765</link>
      <description>Hi Art, this is interesting. It also seems to work on my test data; however, I'm not too familiar with the functions you used. I'll look them up tomorrow morning. Thanks so much!</description>
      <pubDate>Wed, 15 Aug 2018 04:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486904#M126765</guid>
      <dc:creator>bhong</dc:creator>
      <dc:date>2018-08-15T04:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486916#M126769</link>
      <description>&lt;P&gt;As you have found with this question, and what you will find going forward with that data structure is that it is really not a good structure for programming with.&amp;nbsp; Say for instance you want to find the result which is 3 months before today, how will you do this.&amp;nbsp; Some nice lengthy macro code using macro variables and functions to work it out, which will fall over every run and be horrible for others to read?&amp;nbsp; First, re-model your data into a proper usable format e.g:&lt;/P&gt;
&lt;P&gt;date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; result&lt;/P&gt;
&lt;P&gt;01Jun2011&amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;01Jul2011&amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;What this then gives you is the month/year data (i.e. it is not column names) in a usable, easy to program with format that you can do normal data operations on.&amp;nbsp; Then your question is simply a matter of sum(result) where date between dt_chk and hyst_chk - which by the way should also be numeric dates, the 01 part is irrlevant, the point is that date numerics are used to make calcualtions and date manipulations a lot easier.&amp;nbsp; It is no trouble at all to display numeric dates in any format you like, but behind the scenes its always best to keep as numerics.&lt;/P&gt;
&lt;P&gt;I assume this is some sort of learning task, but step one of any learning should be learning the fundamentals, and one of those is good data modelling (another of these is learning not to code all in uppercase which makes reading code harder as it is shouting at us!).&lt;/P&gt;</description>
      <pubDate>Wed, 15 Aug 2018 08:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486916#M126769</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-15T08:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating SAS Arrays Using Column Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486957#M126794</link>
      <description>&lt;P&gt;If I understand you correctly, you want to sum a number of variables, based on whether the variable name (which indicates a month) falls within a certain period.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If (and only if) your variables occur in the table in the correct order, with no other variables in between, you can try something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
	SET mem(OBS=1); 
	DT_CHECK = intnx('month', EVENT_DT, -10);
	DT_CHECK_STR = cats('Y', put(DT_CHECK, YEAR2.), '_', 'M', month(DT_CHECK));
	HYST_DT_STR = cats('Y', put(EVENT_DT, YEAR2.), '_', 'M', month(EVENT_DT));


	ARRAY vars[*] Y80_M1--Y12_M3;
	DO i=1 TO dim(vars) until(varname(vars(i))=DT_CHECK_STR);
           end;
        do i=i to dim(vars) until(varname(vars(i))=HYST_DT_STR);
          sum_n+vars{i};  
          end;

	FORMAT DT_CHECK mmddyy10.;&lt;BR /&gt;        drop i;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The first DO loop finds the variable that matches DT_CHECK_STR, the second sums all the variables up to (and including) the one matching HYST_DT_STR.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, it should also be possible to calculate the starting index:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  set mem;
  array vars(*) Y80_M1--Y12_M3;
  DT_CHECK=intnx('month',EVENT_DT,-10);
  start=intck('month','01jan1980'd,DT_CHECK)+1;
  if start&amp;lt;1 then do;
    error 'Start date out of range';
    delete;
    end;
  else if start&amp;gt;dim(vars)-10 then do;
    error 'End date out of range';
    delete;
    end;
  do _N_=start to start+10;
    sum_n+vars(_N_);
    end;
  format DT_CHECK mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This method is faster, and it is also safer, due to the range checks. Both methods assume that there are columns for all the months in all the years.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Aug 2018 11:53:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-SAS-Arrays-Using-Column-Values/m-p/486957#M126794</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-08-15T11:53:10Z</dc:date>
    </item>
  </channel>
</rss>

