BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Patrick
Opal | Level 21

Hi Experts,
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?

What I'm dealing with are Excel sources for which I'd like to implement code as robust as possible.

 

I'd like code that can deal with:

  • no column header defined (=cell A1 is empty) -> character variable with name A
  • column header defined -> variable has name of column header
  • all cells empty -> character variable
  • cells with only digits -> numerical variable

What I'm currently doing and what will work for my data is:

libname src xlsx "<path>/<name>.xlsx";
data want;
  set src.sheet;
  if substr(cats(of _all_),1,1)='#' then delete;
run; 

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).

libname src xlsx "<path>/<name>.xlsx";
data want;
  set src.sheet;
  array allvars {*} _all_;
  if cats(allvars[1])='#' then delete;
run;

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
/*
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);
if i=&position then leave; end; end; if strip(vvaluex(vname))=:'A' then flag='Y'; run;

View solution in original post

6 REPLIES 6
Ksharp
Super User

Kurt has already given you the solution. 

Also could try CALL VNEXT() if you want the variable name in PDV !

 

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;
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=
Patrick
Opal | Level 21

Thank you for your time @Ksharp and @Kurt_Bremser looking into my question and providing an answer.

I know that I could query the dictionary tables but as stated in my question "I'm not after alternative code like querying the dictionary tables..."

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.

Ksharp
Super User
/*
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);
if i=&position then leave; end; end; if strip(vvaluex(vname))=:'A' then flag='Y'; run;
Patrick
Opal | Level 21

@Ksharp 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. 

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 &position = 3).

%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=&position then leave;
      end;
    end;

  if vvaluex(vname)=:'A' then flag='Y';
run;

 

Ksharp
Super User
Opps. Patrick, I forgot to test other 'position'. As your wish I have changed my code as yours.

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1483 views
  • 3 likes
  • 3 in conversation