<?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: How do I bring back a number of variables from a sas dataset without having to repeat them in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716133#M221272</link>
    <description>&lt;P&gt;The superior method is to have a variable that holds the date of interest such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;product date analysisvar&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With an actual SAS date value, numeric and a date format applied that makes sense, then you can filter data based on range easily such as&lt;BR /&gt;&lt;BR /&gt;Data need;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where '01Jan2011'd le date le '01Oct2015'd;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF, and this can be a very &lt;STRONG&gt;big&lt;/STRONG&gt; if, the variables you want are in adjacent columns you can use Keep= Var1 - - Var23. The two dashes is a sequential list. But if you have variables: var1 othervar1 var2 othervar2 ... var23 othervar23 you would have the 23 var and 22 of the othervar variables kept.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If variable names have a common prefix and an actual &lt;STRONG&gt;sequential&lt;/STRONG&gt; suffix then you can use: Keep Var1 - Var10. Which will keep variables Var1, Var2 ..., Var10. However your "date" names won't work well because crossing year boundaries is not sequential: Var201912 is followed with Var202001 which has a gap of about 89 sequential numbers.&lt;/P&gt;</description>
    <pubDate>Tue, 02 Feb 2021 16:58:00 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-02-02T16:58:00Z</dc:date>
    <item>
      <title>How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716122#M221263</link>
      <description>&lt;P&gt;Hi, I am using SAS again after just over a year at a new company and I know that this is possible and can't remember if it needs to be a loop within a macro&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I have are datasets that hold a number of variables with the year and month on the end and I only want to bring back those that are relevant to the calculation period I want to look at. Example table is shown below but covers about 50 months of results in the real version:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Product_Number&amp;nbsp; Analysis_202001 Analysis 202002 Analysis 202003 Analysis 202004 Analysis 202005 Analysis 202006&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want to be able to do is put a date variable in the datastep as my start date e.g. 202001 and an end date of 202004 which then only brings back those columns in my new dataset&amp;nbsp; along with the product_number).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I currently say in the datastep is this:&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;set temp (keep = product_number analysis_202001 analysis_202002 analysis_202003 analysis_202004);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This obviously doesn't allow me to change this depending on the period I am looking at. I know there is a solution to this but my brain has frozen and just isn't letting me get the result I want so any help would be much appreciated&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 16:39:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716122#M221263</guid>
      <dc:creator>djk200399</dc:creator>
      <dc:date>2021-02-02T16:39:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716129#M221269</link>
      <description>&lt;P&gt;Here is a reference that illustrates how to refer to variables and datasets in a short cut list:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use short cut references, in your case use the colon and Year most likely.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;keep = product_number analysis_2020: 

keep = product_number analysis_202001-analysis_202012&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;Also, this an example of why it's a bad idea to store data like this, use a long format so you can very easily add a WHERE statement.&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/367385"&gt;@djk200399&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I am using SAS again after just over a year at a new company and I know that this is possible and can't remember if it needs to be a loop within a macro&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I have are datasets that hold a number of variables with the year and month on the end and I only want to bring back those that are relevant to the calculation period I want to look at. Example table is shown below but covers about 50 months of results in the real version:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Product_Number&amp;nbsp; Analysis_202001 Analysis 202002 Analysis 202003 Analysis 202004 Analysis 202005 Analysis 202006&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I want to be able to do is put a date variable in the datastep as my start date e.g. 202001 and an end date of 202004 which then only brings back those columns in my new dataset&amp;nbsp; along with the product_number).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I currently say in the datastep is this:&lt;/P&gt;
&lt;P&gt;data test;&lt;/P&gt;
&lt;P&gt;set temp (keep = product_number analysis_202001 analysis_202002 analysis_202003 analysis_202004);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This obviously doesn't allow me to change this depending on the period I am looking at. I know there is a solution to this but my brain has frozen and just isn't letting me get the result I want so any help would be much appreciated&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 16:50:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716129#M221269</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-02-02T16:50:46Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716130#M221270</link>
      <description>&lt;P&gt;To do what you want you just need some method to generate the list of names.&amp;nbsp; For example just use the INTNX() and INTCK() functions to allow you to generate one name per month in the interval.&lt;/P&gt;
&lt;P&gt;So code like this will create a dataset with the 5 months in the interval Jan 2020 thru April 2020.&amp;nbsp; If you want to eliminate the first month to get the number down to four then start the OFFSET variable from 1 instead of 0.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names ;
  length start end offset date 8 name $32.;
  format stat end date yymmn6.;
  start='01JAN2020'd;
  end='01APR2020'd;
  do offset=0 to intck('month',start,end);
     date = intnx('month',start,offset);
     name=cats('Analysis_',put(date,yymmn6.));
     output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Once you know how to generate the list it is easy to put it into a macro variable to use in a KEEP statement.&amp;nbsp; For example with PROC SQL INTO clause.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select name into :names separated by '  '
  from names;
quit;
data test;
  set temp (keep = product_number &amp;amp;names);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Feb 2021 16:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716130#M221270</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-02T16:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716133#M221272</link>
      <description>&lt;P&gt;The superior method is to have a variable that holds the date of interest such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;product date analysisvar&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With an actual SAS date value, numeric and a date format applied that makes sense, then you can filter data based on range easily such as&lt;BR /&gt;&lt;BR /&gt;Data need;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where '01Jan2011'd le date le '01Oct2015'd;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF, and this can be a very &lt;STRONG&gt;big&lt;/STRONG&gt; if, the variables you want are in adjacent columns you can use Keep= Var1 - - Var23. The two dashes is a sequential list. But if you have variables: var1 othervar1 var2 othervar2 ... var23 othervar23 you would have the 23 var and 22 of the othervar variables kept.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If variable names have a common prefix and an actual &lt;STRONG&gt;sequential&lt;/STRONG&gt; suffix then you can use: Keep Var1 - Var10. Which will keep variables Var1, Var2 ..., Var10. However your "date" names won't work well because crossing year boundaries is not sequential: Var201912 is followed with Var202001 which has a gap of about 89 sequential numbers.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 16:58:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716133#M221272</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-02-02T16:58:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716137#M221275</link>
      <description>&lt;P&gt;Transpose to a long layout, and you can use simple WHERE conditions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data wide;
input
  Product_Number
  Analysis_202001
  Analysis_202002
  Analysis_202003
  Analysis_202004
;
datalines;
1 10 10 20 10        
2  5 20 20  5
3 10  5  5  5
;

proc transpose
  data=wide
  out=long (rename=(col1=analysis))
;
by product_number;
var analysis:;
run;

data want;
set long;
period = input(scan(_name_,2,"_"),yymmn6.);
format period yymmn6.;
drop _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By extracting the period from the variable name, and storing it as SAS date, you open yourself up to the use of the SAS date functions, e.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where intnx('month',&amp;amp;refdate.,-3,'b') le period le intnx('month',&amp;amp;refdate.,3,'e');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which handles year boundaries without hassle.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 17:04:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716137#M221275</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-02T17:04:41Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716142#M221278</link>
      <description>&lt;P&gt;There is a trick that could make the use of a variable list possible. It could works because of these specific things about this example:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You wanted to use KEEP= dataset option on the input dataset&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Your variables have numeric suffixes on the dataset name&lt;/LI&gt;
&lt;LI&gt;You are not using any other KEEP=, DROP= or RENAME= dataset options.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;SAS has an option named DKRICOND (Drop Keep Rename Input CONDition) that controls what it does when you reference a non-existent variable with those options when reading from a dataset.&amp;nbsp; Normal default is to error when the variable is not found But you can reduce that to just a warning or no warning at all.&amp;nbsp; With that option you can then use a variable list and the fact that 202013 suffix does not exist will not cause an error.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length analysis_202001-analysis_202012 analysis_202101-analysis_202112 8;
run;

%let optsave= %sysfunc(getoption(dkricond,keyword));
options dkricond=nowarn;
data want;
  set have (keep=analysis_202010 - analysis_202102 );
run;
options &amp;amp;optsave;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Feb 2021 17:16:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716142#M221278</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-02T17:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716159#M221286</link>
      <description>&lt;P&gt;If you always analyze a range of consecutively named variables, try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Set the macro variable value to the correct variable range */
%let range=analysis_202001 - analysis_202004;
data test;
   /* Wherever you need the variable list, reference the macro variable. */
   set temp (keep = product_number &amp;amp;range);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 17:56:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/716159#M221286</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2021-02-02T17:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/717951#M222105</link>
      <description>&lt;P&gt;Tom,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for this as meant I didn't have to do a long winded keep statement every time.&lt;/P&gt;&lt;P&gt;Is there a way for me to then use these names within a calculation.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have attached a mini version of the closing_bals_tester dataset and attached it as a csv.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to be able to do the big calculation below but struggling as obviously I have created some in the names piece. How would I macro or loop this? Apologies for asking you as a reply but I just wondered if I could use some of the elements created in the earlier macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let fstmonth = '31Oct2020'd;&lt;BR /&gt;%let lstmonth = '31Dec2025'd;&lt;/P&gt;&lt;P&gt;data names ;&lt;BR /&gt;length start end offset date 8 name $32.;&lt;BR /&gt;format stat end date yymmn6.;&lt;BR /&gt;start=&amp;amp;fstmonth;&lt;BR /&gt;end=&amp;amp;lstmonth;&lt;BR /&gt;do offset=0 to intck('month',start,end);&lt;BR /&gt;date = intnx('month',start,offset);&lt;BR /&gt;name=cats('cinr_',put(date,yymmn6.));&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select name into :names separated by ' '&lt;BR /&gt;from names;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set ciandnr1 (keep = brand no1 product_group &amp;amp;names);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/*Take the last actual closing balance */&lt;BR /&gt;data Last_Actual_Product_Balances;&lt;BR /&gt;set sav.sepoct20&lt;BR /&gt;(keep=ProdRepGroup prodrepgroupid&lt;BR /&gt;LedgerBal_oct20);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;/*Merge the tables */&lt;BR /&gt;proc sql;&lt;BR /&gt;create table closing_bals_calc as&lt;BR /&gt;select a.*,&lt;BR /&gt;b.*&lt;BR /&gt;from Last_Actual_product_Balances as a&lt;BR /&gt;left join test as b on a.prodrepgroupid=b.no1;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;/* Now do the calcs which should be closing balance for the last actual month&lt;BR /&gt;and the rest are the result of the prior month plus the ci and net receipts-&lt;BR /&gt;we need to put something in to only run this for the months we want to choose&lt;BR /&gt;as we did in earlier macro */&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data closing_bals_calca;&lt;BR /&gt;set closing_bals_calc;&lt;BR /&gt;bal_202010 = ledgerbal_Oct20 / 1000000;&lt;BR /&gt;bal_202011 = bal_202010 + cinr_202011;&lt;BR /&gt;bal_202012 = bal_202011 + cinr_202012;&lt;BR /&gt;bal_202101 = bal_202012 + cinr_202101;&lt;BR /&gt;bal_202102 = bal_202101 + cinr_202102;&lt;BR /&gt;bal_202103 = bal_202102 + cinr_202103;&lt;BR /&gt;bal_202104 = bal_202103 + cinr_202104;&lt;BR /&gt;bal_202105 = bal_202104 + cinr_202105;&lt;BR /&gt;bal_202106 = bal_202105 + cinr_202106;&lt;BR /&gt;bal_202107 = bal_202106 + cinr_202107;&lt;BR /&gt;bal_202108 = bal_202107 + cinr_202108;&lt;BR /&gt;bal_202109 = bal_202108 + cinr_202109;&lt;BR /&gt;bal_202110 = bal_202109 + cinr_202110;&lt;BR /&gt;bal_202111 = bal_202110 + cinr_202111;&lt;BR /&gt;bal_202112 = bal_202111 + cinr_202112;&lt;BR /&gt;bal_202201 = bal_202112 + cinr_202201;&lt;BR /&gt;bal_202202 = bal_202201 + cinr_202202;&lt;BR /&gt;bal_202203 = bal_202202 + cinr_202203;&lt;BR /&gt;bal_202204 = bal_202203 + cinr_202204;&lt;BR /&gt;bal_202205 = bal_202204 + cinr_202205;&lt;BR /&gt;bal_202206 = bal_202205 + cinr_202206;&lt;BR /&gt;bal_202207 = bal_202206 + cinr_202207;&lt;BR /&gt;bal_202208 = bal_202207 + cinr_202208;&lt;BR /&gt;bal_202209 = bal_202208 + cinr_202209;&lt;BR /&gt;bal_202210 = bal_202209 + cinr_202210;&lt;BR /&gt;bal_202211 = bal_202210 + cinr_202211;&lt;BR /&gt;bal_202212 = bal_202211 + cinr_202212;&lt;BR /&gt;bal_202301 = bal_202212 + cinr_202301;&lt;BR /&gt;bal_202302 = bal_202301 + cinr_202302;&lt;BR /&gt;bal_202303 = bal_202302 + cinr_202303;&lt;BR /&gt;bal_202304 = bal_202303 + cinr_202304;&lt;BR /&gt;bal_202305 = bal_202304 + cinr_202305;&lt;BR /&gt;bal_202306 = bal_202305 + cinr_202306;&lt;BR /&gt;bal_202307 = bal_202306 + cinr_202307;&lt;BR /&gt;bal_202308 = bal_202307 + cinr_202308;&lt;BR /&gt;bal_202309 = bal_202308 + cinr_202309;&lt;BR /&gt;bal_202310 = bal_202309 + cinr_202310;&lt;BR /&gt;bal_202311 = bal_202310 + cinr_202311;&lt;BR /&gt;bal_202312 = bal_202311 + cinr_202312;&lt;BR /&gt;bal_202401 = bal_202312 + cinr_202401;&lt;BR /&gt;bal_202402 = bal_202401 + cinr_202402;&lt;BR /&gt;bal_202403 = bal_202402 + cinr_202403;&lt;BR /&gt;bal_202404 = bal_202403 + cinr_202404;&lt;BR /&gt;bal_202405 = bal_202404 + cinr_202405;&lt;BR /&gt;bal_202406 = bal_202405 + cinr_202406;&lt;BR /&gt;bal_202407 = bal_202406 + cinr_202407;&lt;BR /&gt;bal_202408 = bal_202407 + cinr_202408;&lt;BR /&gt;bal_202409 = bal_202408 + cinr_202409;&lt;BR /&gt;bal_202410 = bal_202409 + cinr_202410;&lt;BR /&gt;bal_202411 = bal_202410 + cinr_202411;&lt;BR /&gt;bal_202412 = bal_202411 + cinr_202412;&lt;BR /&gt;bal_202501 = bal_202412 + cinr_202501;&lt;BR /&gt;bal_202502 = bal_202501 + cinr_202502;&lt;BR /&gt;bal_202503 = bal_202502 + cinr_202503;&lt;BR /&gt;bal_202504 = bal_202503 + cinr_202504;&lt;BR /&gt;bal_202505 = bal_202504 + cinr_202505;&lt;BR /&gt;bal_202506 = bal_202505 + cinr_202506;&lt;BR /&gt;bal_202507 = bal_202506 + cinr_202507;&lt;BR /&gt;bal_202508 = bal_202507 + cinr_202508;&lt;BR /&gt;bal_202509 = bal_202508 + cinr_202509;&lt;BR /&gt;bal_202510 = bal_202509 + cinr_202510;&lt;BR /&gt;bal_202511 = bal_202510 + cinr_202511;&lt;BR /&gt;bal_202512 = bal_202511 + cinr_202512;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate any help / guidance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;David&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 17:07:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/717951#M222105</guid>
      <dc:creator>djk200399</dc:creator>
      <dc:date>2021-02-09T17:07:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/717955#M222106</link>
      <description>&lt;P&gt;First comment is that if you need to do calculations like that you have structured your dataset wrong.&amp;nbsp; The date should be in a variable instead being part of a variable name. So in your case instead of N observation with 20 BAL... variables and 20 CINR.. variables you will have three variables DATE, BAL and CINR and 20*N observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But given your current request just use ARRAY statements in your dataset and loop over the index into the array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data closing_bals_calca;
  set closing_bals_calc;
  array bal bal_202010 bal_202011 ..... ;
  array cinr cinr_202011 cinr_202012 ... ;
  do index=1 to dim(bal);
    if index=1 then bal[index] = ledgerbal_Oct20 / 1000000;
    else bal[index]=bal[index] + cinr[index-1];
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To make so you don't have to type all of the names into the ARRAY statements make two macros variables.&amp;nbsp; One to contain the list of BAL variables and the other the list of CINR variables . So then your ARRAY statements can look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array bal &amp;amp;bal ;
  array cinr &amp;amp;cinr ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 17:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/717955#M222106</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-09T17:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/718170#M222207</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried using your code which gave me the first bal_202010 result but thereafter each of the results was a missing calculation. Here is the code I ran (slight tweak as I want the first result to be ledgerbal / 1000000 + the same month's result of cinr. The next calc should be the result of what we have just created + cinr_202011 etc. but all come back as missing even though there are numeric values in there :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data closing_bals_calcz;&lt;BR /&gt;set closing_bals_calc;&lt;BR /&gt;array bal bal_202010 bal_202011 bal202012 bal_202101;&lt;BR /&gt;array cinr cinr_202010 cinr_202011 cinr_202112 cinr_202101 ;&lt;BR /&gt;do index=1 to dim(bal);&lt;BR /&gt;if index=1 then bal[index] = ledgerbal_Oct20 / 1000000+cinr[index];&lt;BR /&gt;else bal[index]=bal[index] + cinr[index];&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 09:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/718170#M222207</guid>
      <dc:creator>djk200399</dc:creator>
      <dc:date>2021-02-10T09:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/718183#M222213</link>
      <description>&lt;P&gt;All your troubles stem from a catastrophic design failure (keeping data in structure). As long as you permeate this, you will have issues with stupid code and unnecessary work.&lt;/P&gt;
&lt;P&gt;Starting with the csv you provided, I first bring that into usable shape:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data closing_bals_calc;
infile
  "/folders/myfolders/CLOSING_BALS_TESTER.csv"
  dlm=","
  dsd
  truncover
  firstobs=2
;
input LedgerBal_Oct20 cinr_202011 cinr_202012 cinr_202101 cinr_202102 no1;
run;

proc transpose
  data=closing_bals_calc
  out=l1 (rename=(col1=cinr))
;
by no1;
var cinr:;
run;

data long;
set l1;
period = input(scan(_name_,2,'_'),yymmn6.);
format period yymmn6.;
drop _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now the code looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge 
  closing_bals_calc (keep=no1 ledgerbal_oct20)
  long
;
by no1;
retain balance;
if first.no1
then balance = LedgerBal_Oct20 / 1000000 + cinr;
else balance = balance + cinr;
drop ledgerbal_oct20;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As I see from your code that the design failure seems to be present in most of your data, you need to correct it there, as most of the code you posted would not be necessary (the same functionality would then be served by simple WHERE conditions).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maxim 19: Long Beats Wide.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 11:16:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/718183#M222213</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-10T11:16:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do I bring back a number of variables from a sas dataset without having to repeat them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/718192#M222217</link>
      <description>&lt;P&gt;Thank you for this Kurt, the problem is that people are keying into spreadsheets which then get inputted so we need to use all their fields to calc some elements, hence your transpose to a long file does the job perfectly bringing it in.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am now thinking I will export the results out and then use an excel macro to convert them back to the way they see them currently to avoid transposing it back again in SAS&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 11:49:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-bring-back-a-number-of-variables-from-a-sas-dataset/m-p/718192#M222217</guid>
      <dc:creator>djk200399</dc:creator>
      <dc:date>2021-02-10T11:49:39Z</dc:date>
    </item>
  </channel>
</rss>

