<?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: (Beginner SAS) How to do double for loop to generate keep list? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Beginner-SAS-How-to-do-double-for-loop-to-generate-keep-list/m-p/549100#M8610</link>
    <description>&lt;P&gt;I like Approach #2 best, and would write it:&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 :name_list separated by " "
from sashelp.vcolumn 
where memname = 'WORLDDATA' and prxmatch("/_(usa|canada|uk|japan|southafrica)_2017_(population|crimerate|gnp)/i", name);
quit;

%put &amp;amp;name_list.;

data want;
set worldData(keep=&amp;amp;name_list.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 07 Apr 2019 05:07:29 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2019-04-07T05:07:29Z</dc:date>
    <item>
      <title>(Beginner SAS) How to do double for loop to generate keep list?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Beginner-SAS-How-to-do-double-for-loop-to-generate-keep-list/m-p/549075#M8602</link>
      <description>&lt;P&gt;I have a very large dataset with over 1000 columns, with column names formatted like this:&lt;/P&gt;&lt;PRE&gt;WORLDDATA.table2_usa_2017_population
WORLDDATA.table2_japan_2017_gnp&lt;/PRE&gt;&lt;P&gt;I only need to keep a subset of these parameters for a select few countries. I specify the custom lists:&lt;/P&gt;&lt;PRE&gt;%let list1 = usa canada uk japan southafrica;
%let list2 = population crimerate gnp;&lt;/PRE&gt;&lt;P&gt;How do I do a double for loop like so:&lt;/P&gt;&lt;PRE&gt;&amp;nbsp; param_list = []
&amp;nbsp; for (i in list1) {
&amp;nbsp; &amp;nbsp; for (j in list2) {
&amp;nbsp; &amp;nbsp; &amp;nbsp; param_name = WORLDDATA.table2_{list1[i]}_2017_{list2[j]}
&amp;nbsp; &amp;nbsp; &amp;nbsp; param_list.append(param_name)
&amp;nbsp; &amp;nbsp; }
&amp;nbsp; }&lt;/PRE&gt;&lt;P&gt;such that I can use param_list in&lt;/P&gt;&lt;PRE&gt;data final_dataset;
&amp;nbsp; set WORLDDATA.table2;
&amp;nbsp; keep {param_list};
run;&lt;/PRE&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Sat, 06 Apr 2019 23:04:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Beginner-SAS-How-to-do-double-for-loop-to-generate-keep-list/m-p/549075#M8602</guid>
      <dc:creator>RogerJones</dc:creator>
      <dc:date>2019-04-06T23:04:39Z</dc:date>
    </item>
    <item>
      <title>Re: (Beginner SAS) How to do double for loop to generate keep list?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Beginner-SAS-How-to-do-double-for-loop-to-generate-keep-list/m-p/549076#M8603</link>
      <description>&lt;P&gt;If you know the variables and the naming structure is pretty fixed you have a few options that I see:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Generate a dynamic list using a macro approach&lt;/P&gt;
&lt;P&gt;2. Select the names using pattern matching from the list, assumed fixed pattern of table2_&lt;STRONG&gt;COUNTRY&lt;/STRONG&gt;_YEAR_&lt;STRONG&gt;STATISTIC.&amp;nbsp;&lt;/STRONG&gt;Use SCAN() to extract those portion of the name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Reformat the data such that it's in a long/longer format and then filter/manipulate it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the long run, I suspect #3 is better because the countries will be rows rather than in the variable name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Approach #2 - note the index (2/4) may be wrong, you should test this.&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 :name_list separated by " "
from sashelp.vcolumn where memname = 'WORLDDATA'
and lower(scan(name, 2, "_.")) in ('use', 'canada', 'japan')
and lower(scan(name, 4, "_.")) in ('population', 'crimerate', 'gap');
quit;

%put &amp;amp;name_list.;

data want;
set worldData;
keep &amp;amp;name_list;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For an example of #3 I'd need more information on the sample structure.&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269631"&gt;@RogerJones&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a very large dataset with over 1000 columns, with column names formatted like this:&lt;/P&gt;
&lt;PRE&gt;WORLDDATA.table2_usa_2017_population
WORLDDATA.table2_japan_2017_gnp&lt;/PRE&gt;
&lt;P&gt;I only need to keep a subset of these parameters for a select few countries. I specify the custom lists:&lt;/P&gt;
&lt;PRE&gt;%let list1 = usa canada uk japan southafrica;
%let list2 = population crimerate gnp;&lt;/PRE&gt;
&lt;P&gt;How do I do a double for loop like so:&lt;/P&gt;
&lt;PRE&gt;&amp;nbsp; param_list = []
&amp;nbsp; for (i in list1) {
&amp;nbsp; &amp;nbsp; for (j in list2) {
&amp;nbsp; &amp;nbsp; &amp;nbsp; param_name = WORLDDATA.table2_{list1[i]}_2017_{list2[j]}
&amp;nbsp; &amp;nbsp; &amp;nbsp; param_list.append(param_name)
&amp;nbsp; &amp;nbsp; }
&amp;nbsp; }&lt;/PRE&gt;
&lt;P&gt;such that I can use param_list in&lt;/P&gt;
&lt;PRE&gt;data final_dataset;
&amp;nbsp; set WORLDDATA.table2;
&amp;nbsp; keep {param_list};
run;&lt;/PRE&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Apr 2019 23:26:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Beginner-SAS-How-to-do-double-for-loop-to-generate-keep-list/m-p/549076#M8603</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-04-06T23:26:16Z</dc:date>
    </item>
    <item>
      <title>Re: (Beginner SAS) How to do double for loop to generate keep list?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Beginner-SAS-How-to-do-double-for-loop-to-generate-keep-list/m-p/549100#M8610</link>
      <description>&lt;P&gt;I like Approach #2 best, and would write it:&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 :name_list separated by " "
from sashelp.vcolumn 
where memname = 'WORLDDATA' and prxmatch("/_(usa|canada|uk|japan|southafrica)_2017_(population|crimerate|gnp)/i", name);
quit;

%put &amp;amp;name_list.;

data want;
set worldData(keep=&amp;amp;name_list.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Apr 2019 05:07:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Beginner-SAS-How-to-do-double-for-loop-to-generate-keep-list/m-p/549100#M8610</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-04-07T05:07:29Z</dc:date>
    </item>
    <item>
      <title>Re: (Beginner SAS) How to do double for loop to generate keep list?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Beginner-SAS-How-to-do-double-for-loop-to-generate-keep-list/m-p/549149#M8621</link>
      <description>&lt;P&gt;I think you are seeking something more generic, along the lines of a macro:&lt;BR /&gt;&lt;BR /&gt;%macro vlist (list1=, list2=) ;&lt;BR /&gt;%local j k ;&lt;BR /&gt;%do j=1 %to %sysfunc(countw(&amp;amp;list1) ) ;&lt;BR /&gt;%do k=1 %to %sysfunc(countw(&amp;amp;list2) ) ;&lt;BR /&gt;table2_%scan(&amp;amp;list1, &amp;amp;j)_2017_%scan(&amp;amp;list2, &amp;amp;k)&lt;BR /&gt;%end;&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;%mend vlist;&lt;BR /&gt;&lt;BR /&gt;Then you can use the macro where you want the list of names such as:&lt;BR /&gt;&lt;BR /&gt;drop %vlist (list1= usa canada uk japan southafrica,&lt;BR /&gt;list2 = population crime rate gnp) ;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Apr 2019 17:55:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Beginner-SAS-How-to-do-double-for-loop-to-generate-keep-list/m-p/549149#M8621</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-04-08T17:55:50Z</dc:date>
    </item>
  </channel>
</rss>

