<?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 to reference a variable if I only know the position in the PDV? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/843988#M333664</link>
    <description>&lt;P&gt;You can get the name from DICTIONARY.COLUMNS, or use the OPEN and VARNAME functions in a data step.&lt;/P&gt;</description>
    <pubDate>Sun, 13 Nov 2022 08:11:32 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-11-13T08:11:32Z</dc:date>
    <item>
      <title>How to reference a variable if I only know the position in the PDV?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/843984#M333662</link>
      <description>&lt;P&gt;Hi Experts,&lt;BR /&gt;Is there any syntax that allows me to address a variable in a data step if the only thing I know is that it's the first variable in the PDV?&lt;/P&gt;
&lt;P&gt;What I'm dealing with are Excel sources for which I'd like to implement code as robust as possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd like code that can deal with:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;no column header defined (=cell A1 is empty) -&amp;gt; character variable with name A&lt;/LI&gt;
&lt;LI&gt;column header defined -&amp;gt; variable has name of column header&lt;/LI&gt;
&lt;LI&gt;all cells empty -&amp;gt; character variable&lt;/LI&gt;
&lt;LI&gt;cells with only digits -&amp;gt; numerical variable&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;What I'm currently doing and what will work for my data is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname src xlsx "&amp;lt;path&amp;gt;/&amp;lt;name&amp;gt;.xlsx";
data want;
  set src.sheet;
  if substr(cats(of _all_),1,1)='#' then delete;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I'd like to do is something like below (which of course doesn't work if there are numerical and character variables in the pdv).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname src xlsx "&amp;lt;path&amp;gt;/&amp;lt;name&amp;gt;.xlsx";
data want;
  set src.sheet;
  array allvars {*} _all_;
  if cats(allvars[1])='#' then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm not after alternative code like querying the dictionary tables etc. but really curious if there is another way to reference a variable without having to know the name in advance other than using an array.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2022 06:24:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/843984#M333662</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-11-13T06:24:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to reference a variable if I only know the position in the PDV?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/843988#M333664</link>
      <description>&lt;P&gt;You can get the name from DICTIONARY.COLUMNS, or use the OPEN and VARNAME functions in a data step.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2022 08:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/843988#M333664</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-11-13T08:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to reference a variable if I only know the position in the PDV?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/843996#M333666</link>
      <description>&lt;P&gt;Kurt has already given you the solution.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also could try CALL VNEXT() if you want the variable name in PDV !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
 set sashelp.class;
length vname $ 40;
do i=1 by 1 until(missing(vname));
 call vnext(vname);
 put i= vname=;
end;

stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;i=1 vname=Name
i=2 vname=Sex
i=3 vname=Age
i=4 vname=Height
i=5 vname=Weight
i=6 vname=vname
i=7 vname=i
i=8 vname=_ERROR_
i=9 vname=_N_
i=10 vname=
&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Nov 2022 09:49:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/843996#M333666</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-11-13T09:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to reference a variable if I only know the position in the PDV?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/843997#M333667</link>
      <description>&lt;P&gt;Thank you for your time&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;looking into my question and providing an answer.&lt;/P&gt;
&lt;P&gt;I know that I could query the dictionary tables but as stated in my question "&lt;SPAN&gt;&lt;STRONG&gt;I'm not after alternative code like querying the dictionary tables...&lt;/STRONG&gt;"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I was wondering if there is any data step syntax I don't know of that lets me reference a variable by position in the PDV. I don't believe this exists other than via the varname() function and DDV but then there are still many things I don't know.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2022 10:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/843997#M333667</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-11-13T10:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to reference a variable if I only know the position in the PDV?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/844077#M333716</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
Hi Patrick, How about this one ?
*/
%let position=1;

data have;
 set sashelp.class;

if _n_=1 then do;
length vname $ 40;
retain vname;
do i=1 by 1 until(missing(vname));
  call vnext(vname);&lt;BR /&gt;  if i=&amp;amp;position then leave;
end;
end;

if strip(vvaluex(vname))=:'A' then flag='Y';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Nov 2022 11:44:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/844077#M333716</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-11-15T11:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to reference a variable if I only know the position in the PDV?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/844306#M333788</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;Yes, I like that. It's dynamic but still simple and the source variable can be either character or numeric with the code not even writing some type conversion note.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe you need a small change to your proposed code to ensure that call vnext() gets always executed until the condition is met (like for &amp;amp;position = 3).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let position=1;

data have;
  set sashelp.class;

  if _n_=1 then
    do;
      length vname $ 32;
      retain vname;
      do i=1 by 1 until(missing(vname));
        call vnext(vname);
        if i=&amp;amp;position then leave;
      end;
    end;

  if vvaluex(vname)=:'A' then flag='Y';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2022 20:07:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/844306#M333788</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-11-15T20:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to reference a variable if I only know the position in the PDV?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/844357#M333804</link>
      <description>Opps. Patrick, I forgot to test other 'position'. As your wish I have changed my code as yours.</description>
      <pubDate>Tue, 15 Nov 2022 11:45:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-reference-a-variable-if-I-only-know-the-position-in-the/m-p/844357#M333804</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-11-15T11:45:48Z</dc:date>
    </item>
  </channel>
</rss>

