<?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: Adding a new group of variables with array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721154#M223459</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/197542"&gt;@A_Swoosh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;proc sql to create a variable list first then use the &amp;amp;var_list to use in the array.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't understand why proc sql is required to create a variable list.&amp;nbsp; What can it do in that regard that a data step can't do.&amp;nbsp; And array are a well-defined and easy-to-use capability of data steps.&lt;/P&gt;</description>
    <pubDate>Tue, 23 Feb 2021 01:09:39 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-02-23T01:09:39Z</dc:date>
    <item>
      <title>Adding a new group of variables with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721138#M223448</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am attempting to add a new set of variables using an array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I currently have a dataset that looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines4 dlm='|' missover dsd;
input id val q1_2018_units q1_2018_stdunits q2_2018_stdunits q3_2018_stdunits q4_2018_stdunits;
datalines;
1|11111|214252|214252|149115|78686543|68777|
2|11000|12|24|0|0|3665664|
3|122211|21252|21252|0|0|3656|
4|11331|214252|6677543|0|0|35535|
5|5555|2252|999999|14115|4234554|44566|
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am trying to create a new set of variables that takes the quarter,year, val and stdunits to derive a new set of variables called q1_2018_computed all the way to q4_2018_computed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;q1_2018_computed= q1_2018_stdunits*100/val/365 and so on...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This would essentially add 4 new columns with this computation for each quarter that I can extend to multiple quarter/year combinations&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can I do this with an array and sql?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. I was thinking to do sql to create a variable list; one for existing and one for new variables&lt;/P&gt;
&lt;P&gt;2. Use the 2 arrays and do loop to compute new columns for each based on the formula above?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would that work? I am unclear about creating and incorporating the new variables I want to add into my array and creating them without replacing the old ones.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 00:00:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721138#M223448</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-02-23T00:00:01Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new group of variables with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721146#M223454</link>
      <description>&lt;P&gt;SQL does not allow arrays or loops. Why SQL?&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 00:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721146#M223454</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-02-23T00:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new group of variables with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721147#M223455</link>
      <description>&lt;P&gt;proc sql to create a variable list first then use the &amp;amp;var_list to use in the array.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 00:41:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721147#M223455</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-02-23T00:41:21Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new group of variables with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721152#M223457</link>
      <description>&lt;P&gt;You have just 4 variables per array. You don't&amp;nbsp; need sql to create the list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
      arraiy a {4} q1_2018_units q1_2018_stdunits q2_2018_stdunits q3_2018_stdunits q4_2018_stdunits;
      array b {4) q1_2018_computed q2_2018_computed q3_2018_computed q4_2018_computed;
      do i=1 to  4;
           b(i) = a(i) *100/val/365;
     end;
    drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Feb 2021 01:02:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721152#M223457</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-23T01:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new group of variables with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721153#M223458</link>
      <description>&lt;P&gt;For this example, I have 4 but for my actual dataset I have multiple years and 4 quarters for each year which is why I wanted to create a list. The q1_2018 and so forth carry on from year to year, and I just want to add a suffix of _computed to each one with a computed value based on the formula.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 01:04:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721153#M223458</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-02-23T01:04:20Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new group of variables with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721154#M223459</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/197542"&gt;@A_Swoosh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;proc sql to create a variable list first then use the &amp;amp;var_list to use in the array.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't understand why proc sql is required to create a variable list.&amp;nbsp; What can it do in that regard that a data step can't do.&amp;nbsp; And array are a well-defined and easy-to-use capability of data steps.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 01:09:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721154#M223459</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-02-23T01:09:39Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a new group of variables with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721156#M223460</link>
      <description>&lt;P&gt;Does the below suit your purpose:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	infile datalines4 dlm='|' missover dsd;
	input id val q1_2018_units q1_2018_stdunits q2_2018_stdunits q3_2018_stdunits q4_2018_stdunits q1_2019_stdunits q2_2019_stdunits q3_2019_stdunits q4_2019_stdunits;
	datalines;
1|11111|214252|214252|149115|78686543|6777|214252|14115|7868543|6877|
2|11000|12|24|0|0|366664|24|0|0|365664|
3|122211|21252|21252|0|0|36786|21252|5436|0|656|
4|11331|214252|6677543|0|0|3535|667543|0|0|3535|
5|5555|2252|999999|14115|4234554|446566|9545999|14115|4534554|4445566|
;
run;

%macro test;

%macro _;
%mend _;

%local year j k;

proc sql noprint;
	select min(year) as min_year,max(year) as max_year,count(year) as cnt
		into: min_year TRIMMED, :max_year TRIMMED, :cnt TRIMMED
	from
		(
	select distinct input(scan(name,2,"_"),best12.) as year
		from dictionary.columns
			where libname = 'WORK'
				and memname = 'HAVE');
quit;

%put &amp;amp;=min_year &amp;amp;=max_year &amp;amp;=cnt;

data want;
	set have;
	array q_have{%sysevalf(&amp;amp;cnt.*4)} %do year = &amp;amp;min_year. %to &amp;amp;max_year.;

	%do j = 1 %to 4;
		q&amp;amp;j._&amp;amp;year._stdunits
	%end;
%end;
	;
	array q_want{%sysevalf(&amp;amp;cnt.*4)} %do year = &amp;amp;min_year. %to &amp;amp;max_year.;

	%do k = 1 %to 4;
		q&amp;amp;k._&amp;amp;year._computed
	%end;
%end;
	;
	do i = 1 to dim(q_have);
		q_want{i} = q_have{i}*100/val/365;
	end;

	drop i;
run;

%mend;

%test&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Feb 2021 01:25:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-new-group-of-variables-with-array/m-p/721156#M223460</guid>
      <dc:creator>qoit</dc:creator>
      <dc:date>2021-02-23T01:25:26Z</dc:date>
    </item>
  </channel>
</rss>

