<?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: Create a variable with name based on part of variable names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777716#M247467</link>
    <description>data want;&lt;BR /&gt;set have;&lt;BR /&gt;array _c (*) c:;&lt;BR /&gt;myYear=max(of _c(*));&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Use wild card options on the variable lists.</description>
    <pubDate>Mon, 01 Nov 2021 16:12:30 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-11-01T16:12:30Z</dc:date>
    <item>
      <title>Create a variable with name based on part of variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777703#M247460</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I want to create a global variable with part of its variable name based on values out of a list of variable names in a dataset. In this case, a global variable, max2020, based on the max values of the last 4 letters of variables c2018,c2019,c2029.&lt;/P&gt;
&lt;P&gt;Any help is appreciated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; input ID c2018 c2019 c2020;
datalines;
1 2 3 3
2 4 3 2
3 2 3 4
4 3 2 2
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Nov 2021 15:30:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777703#M247460</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2021-11-01T15:30:56Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable with name based on part of variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777704#M247461</link>
      <description>&lt;P&gt;What to you mean by "global variable"?&amp;nbsp; Do you mean macro variable in the global symbol space?&lt;/P&gt;
&lt;P&gt;What to you want to NAME the macro variable? What value do you want the macro variable to have?&lt;/P&gt;
&lt;P&gt;If you want to variable to be named MAX2020 and have to last variable name in the series of variables with names like cYYYY then perhaps this simple code will do what you want?&lt;/P&gt;
&lt;P&gt;The output of PROC CONTENTS will be sort by the NAME of the variable.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have(keep=c:) out=contents noprint; run;
data _null_;
  set contents end=eof;
  if eof then call symputx('max2020',name);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note this assumes the the cYYYY variables are the only ones that start with the letter C and that they all have the same case for the letter C so that they will sort properly.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Nov 2021 15:39:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777704#M247461</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-01T15:39:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable with name based on part of variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777707#M247462</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input ID c2018 c2019 c2020;
max2020 = max(of c2018-c2020);
datalines;
1 2 3 3
2 4 3 2
3 2 3 4
4 3 2 2
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Like that?&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/19901"&gt;@Solph&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I want to create a global variable with part of its variable name based on values out of a list of variable names in a dataset. In this case, a global variable, max2020, based on the max values of the last 4 letters of variables c2018,c2019,c2029.&lt;/P&gt;
&lt;P&gt;Any help is appreciated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; input ID c2018 c2019 c2020;
datalines;
1 2 3 3
2 4 3 2
3 2 3 4
4 3 2 2
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Nov 2021 15:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777707#M247462</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-01T15:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable with name based on part of variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777714#M247465</link>
      <description>&lt;P&gt;I wasn't clear in my question. What I actually need is grab the last four digits of variable name and find the max value (in this case 2020) and then use that max value as a value in macro parameter (&amp;amp;lastyear) that would let me do work such as&amp;nbsp;&lt;/P&gt;
&lt;P&gt;myyear=max(of c2018-c&amp;amp;lastyear).&lt;/P&gt;
&lt;P&gt;You might ask why not just lastyear=max(c2018-c2020). That's because variables in the list are dynamic, so I need it an automatic process. Tom's solution is closer to what I want. Just need to make it a system variable.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Nov 2021 16:08:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777714#M247465</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2021-11-01T16:08:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable with name based on part of variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777716#M247467</link>
      <description>data want;&lt;BR /&gt;set have;&lt;BR /&gt;array _c (*) c:;&lt;BR /&gt;myYear=max(of _c(*));&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Use wild card options on the variable lists.</description>
      <pubDate>Mon, 01 Nov 2021 16:12:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777716#M247467</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-01T16:12:30Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable with name based on part of variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777718#M247468</link>
      <description>&lt;P&gt;Can you use the prefix wildcard to select just the Cnnnn variables?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  array _c c: ;
  do index=1 to dim(_c);
     n=input(substr(vname(_c[index]),2),4.);
     min=min(min,n);
     max=max(max,n);
  end;
  call symputx('firstyear',min);
  call symputx('lastyear',max);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or will there be a conflict with the names of other variables in your dataset, names like CASEID?&lt;/P&gt;
&lt;P&gt;You can make it a little more bullet proof.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have (keep=_numeric_);
  array _c c: ;
  do index=1 to dim(_c);
    n=input(substr(vname(_c[index]),2),??32.);
    if 1950&amp;lt;= n &amp;lt;= 2050 then do;
      min=min(min,n);
      max=max(max,n);
    end;
  end;
  call symputx('firstyear',min);
  call symputx('lastyear',max);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Nov 2021 16:29:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777718#M247468</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-01T16:29:50Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable with name based on part of variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777720#M247469</link>
      <description>&lt;P&gt;This is the code that works, based on feedback from you all. Really appreciated it. So when there is a new variable c2021 added for a new data cut, it would automatically pick up c2021 when I want to calculate maximum year.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data havex; input id 1 c2018 3-6 c2019 8-11 c2020 13-16;
datalines;
1 2018 2019 2020 
2 2018      2020
3 2018 2019
4 2018
;
proc contents data=havex (keep=c:) out=toc noprint; run;
data _null_ ;
  set toc end=eof;
  if eof then call symputx('lastyr',substr(name,2,4));
run;
data want; set havex;
maxyear=max(of c2018-c&amp;amp;lastyr);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Have data:&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; c2018 c2019 c2020 maxyear&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; 2018&amp;nbsp; 2019&amp;nbsp; &amp;nbsp;2020&amp;nbsp; &amp;nbsp;2020&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; 2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2020&amp;nbsp; &amp;nbsp;2020&lt;BR /&gt;3&amp;nbsp; &amp;nbsp; 2018&amp;nbsp; 2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&lt;BR /&gt;4&amp;nbsp; &amp;nbsp; 2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2018&lt;/P&gt;</description>
      <pubDate>Mon, 01 Nov 2021 16:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777720#M247469</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2021-11-01T16:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable with name based on part of variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777723#M247472</link>
      <description>Re using SYMPUTX to create the macro variable, the following code works as well:&lt;BR /&gt;proc sql; select max(substr(name,2,4)) into: lastyr from toc; quit;</description>
      <pubDate>Mon, 01 Nov 2021 17:07:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-name-based-on-part-of-variable-names/m-p/777723#M247472</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2021-11-01T17:07:31Z</dc:date>
    </item>
  </channel>
</rss>

