<?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 new variables based on part of a variable name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272500#M54210</link>
    <description>&lt;P&gt;Use a PROC MEANS to summarize your data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=have noprint;
var &amp;amp;var_list;
output out=want sum= /autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 23 May 2016 18:20:56 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-05-23T18:20:56Z</dc:date>
    <item>
      <title>Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272194#M54140</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to search all of the variables in my dataset for a particular substring at the end of my variable name, and then make calculations to these variables that satisfy this condition and then create a new variable. In my dataset, I have a few hundred variables overall, and many that end in _population. For&amp;nbsp;example, if I have vars di_population and xi_population and ci_population, I want each to sum each of these by the amount of observations I have in my dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been trying to use:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
set old;
if findc('_population', _all_) then sum;
run;


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but this doesn't seem to the correct way to find the variables that end in _population OR to add these together. Does anyone have any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2016 21:58:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272194#M54140</guid>
      <dc:creator>kevinmc87</dc:creator>
      <dc:date>2016-05-20T21:58:47Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272197#M54141</link>
      <description>&lt;P&gt;SAS doesn't provide a way to work with variables that have suffixes only prefixes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given that it's not 100% clear what you're asking, I'm going to suggest posting sample data and expected output. It's easier than guessing what you're after.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2016 22:07:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272197#M54141</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-20T22:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272198#M54142</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I see that my attachment did not attach.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;name ob_population ob_est x_population x_est c_population c_est
1          50       2           70       5         80       7
2         100       3           150      4         100      10
3          55       5           50       10        75       50&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;what I want is to create a new row that pools this info. So, I want all variables that end in _population to sum. For example, ob_population would now add 50 + 100 + 55 = 205, and I would get a new 'observation' which would be name 4, that contains this. x_population and c_population would also do the same thing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does this make sense?&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2016 22:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272198#M54142</guid>
      <dc:creator>kevinmc87</dc:creator>
      <dc:date>2016-05-20T22:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272199#M54143</link>
      <description>&lt;P&gt;What will you do with the resulting dataset? The behavior you are describing is more typical for a report as summary values inside a dataset with the base values has the potential of being used in such a way the total is more than intended.&lt;/P&gt;
&lt;P&gt;See:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=have noobs;
   sum ob_population x_population c_population;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Depending on what you have done before it may even be possible that the data would make more sense as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Type (with values of Ob X C) Population and Est.&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2016 22:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272199#M54143</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-05-20T22:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272200#M54144</link>
      <description>&lt;P&gt;Ballard,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a many population variables - and I want to somewhat automate this process. I know that I could go through and do that, but I would like a much more efficient way to do this. In addition to the _population vars, I have other vars that I will be conducting other calcuations on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2016 22:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272200#M54144</guid>
      <dc:creator>kevinmc87</dc:creator>
      <dc:date>2016-05-20T22:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272202#M54146</link>
      <description>List all vars in an array, chech if name contains population using a combination of find and vname function. Then add up values. &lt;BR /&gt;&lt;BR /&gt;Or parse names based on suffix - was a question in last 24 hours on extracting the variable names.</description>
      <pubDate>Fri, 20 May 2016 22:50:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272202#M54146</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-20T22:50:19Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272203#M54147</link>
      <description>&lt;P&gt;Thanks, Reeza. Can you expound on how to use the combination of find and vname to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2016 23:01:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272203#M54147</guid>
      <dc:creator>kevinmc87</dc:creator>
      <dc:date>2016-05-20T23:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272205#M54148</link>
      <description>&lt;P&gt;So far, I have this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data region1_2;
	set region1;
	array test [4:513]  y2--y1000;
	do i=1 to dim(test); 
	if find(i,'_population') then ;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I'm stuck on how to incorporate vname function and to sum these. Can you point me in the right direction?&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2016 23:39:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272205#M54148</guid>
      <dc:creator>kevinmc87</dc:creator>
      <dc:date>2016-05-20T23:39:40Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272207#M54150</link>
      <description>&lt;P&gt;You could do it that way but it's inefficient.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would be something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDITtED:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data region1_2;
	set region1;
	array test [4:513]  y2--y1000;
        sum=0;

	do i=1 to dim(test); 
	     if find(vname(test(i)),'_population') then sum= sum+test(i);
        end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Like I mentioned, it's better to get the names once from the SASHELP.VCOLUMN or the Dictionary table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select name into :var_list separated by ", "
from sashelp.vcolumn
where upper(libname)='WORK' and and upper(me name) ='HAVE' and upper(name) like '%_POPULATION';
quit;

data region1_2;
set region1;
sum_want = sum(&amp;amp;var_list);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 21 May 2016 09:30:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272207#M54150</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-21T09:30:38Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272212#M54152</link>
      <description>&lt;PRE&gt;
Add some salt into Rezza's code:



data have;
input name ob_population ob_est x_population x_est c_population c_est;
cards;
1          50       2           70       5         80       7
2         100       3           150      4         100      10
3          55       5           50       10        75       50
;
run;
proc sql noprint;
select name into : var_list separated by ", "
from sashelp.vcolumn
where libname='WORK' and memname='HAVE' and
 lowcase(name) like '%~_population' escape '~';
quit;
%put &amp;amp;var_list ;
data region1_2;
set have;
sum_want = sum(&amp;amp;var_list);
run;


&lt;/PRE&gt;</description>
      <pubDate>Sat, 21 May 2016 03:12:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272212#M54152</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-05-21T03:12:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272472#M54199</link>
      <description>&lt;P&gt;Thanks Ksharp and Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This has been very helpful &amp;nbsp;- especially using the var_list macro. However, I am trying to do something slightly different than the solution you proposed, and I think it is because I did not explain my issue well enough. I am attempting to sum down the columns for each var that ends in _population, and not across all rows that end in _population for each observation. For example, I want to sum down&amp;nbsp;ob_population to get (50+100+55)=205, and create a new var such as ob_population_sum, and continue to do this for x_population, c_population, and so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Essentially, I am trying to create a new row that corresponds to the total of a column summed above it. I've tried using something as:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table region1_3 as select *,
	sum(&amp;amp;var_list) as x_total
	from region1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but I'm still getting a new column that corresponds to summing across rows, not a new row that corresponds to summing down the _population columns. How do I sum down these columns, using the var_list macro?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 23 May 2016 17:15:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272472#M54199</guid>
      <dc:creator>kevinmc87</dc:creator>
      <dc:date>2016-05-23T17:15:05Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272500#M54210</link>
      <description>&lt;P&gt;Use a PROC MEANS to summarize your data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=have noprint;
var &amp;amp;var_list;
output out=want sum= /autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 May 2016 18:20:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272500#M54210</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-05-23T18:20:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272588#M54251</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data region1;
input name ob_population ob_est x_population x_est c_population c_est;
cards;
1          50       2           70       5         80       7
2         100       3           150      4         100      10
3          55       5           50       10        75       50
;
run;
proc sql noprint;
select name into : var_list separated by ", "
from sashelp.vcolumn
where libname='WORK' and memname='HAVE' and
 lowcase(name) like '%~_population' escape '~';
 
select catx(' ','sum(',name,') as ',cats(name,'_sum')) into : var_list1 separated by ", "
from sashelp.vcolumn
where libname='WORK' and memname='HAVE' and
 lowcase(name) like '%~_population' escape '~';

	create table region1_3 as 
	select &amp;amp;var_list1 ,
	sum(sum(&amp;amp;var_list)) as x_total
	from region1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 May 2016 01:01:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/272588#M54251</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-05-24T01:01:08Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on part of a variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/273443#M54487</link>
      <description>&lt;P&gt;Thanks to both of you!&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 23:30:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-part-of-a-variable-name/m-p/273443#M54487</guid>
      <dc:creator>kevinmc87</dc:creator>
      <dc:date>2016-05-26T23:30:49Z</dc:date>
    </item>
  </channel>
</rss>

