BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dsbihill
Obsidian | Level 7

Is there a way to reference a column by position rather then its variable name.  I have multiple variables that have different names but i want to loop through them. 

I have searched quite a bit for this but must be using the wrong terminology because i cant seem to get close to the answer I am looking for.  Is my only option to create an array and

name out all the variables and then step through the array?  Or is there a way to just pick variables 5 - 15 and loop through them.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

It might worth the trouble creating a macro if you have a lot of files.

Sample code:

%let dsn=sashelp.class;

proc contents data=&dsn noprint out=test(keep=name varnum);

data _null_;

  set test(where=(varnum in(3,5)));;

  call symputx(cats('var',varnum),name);

run;

data want;

   set &dsn;

   array _abc(*) &var3--&var5;

   array _new(*) new3-new5;

   do _n_=1 to dim(_abc);

   _new(_n_)=_abc(_n_)*100;

   end;

proc print;run;

Message was edited by: Linlin

View solution in original post

3 REPLIES 3
Reeza
Super User

I think you need to list them out in an array.

If its a one-off then that's the easiest. If its done multiple times, you could query sashelp.vcolumns and create a macro variable out of the names to feed an array but that seems like more work to me.

Depending on what you're doing IML may also be an option.

Linlin
Lapis Lazuli | Level 10

It might worth the trouble creating a macro if you have a lot of files.

Sample code:

%let dsn=sashelp.class;

proc contents data=&dsn noprint out=test(keep=name varnum);

data _null_;

  set test(where=(varnum in(3,5)));;

  call symputx(cats('var',varnum),name);

run;

data want;

   set &dsn;

   array _abc(*) &var3--&var5;

   array _new(*) new3-new5;

   do _n_=1 to dim(_abc);

   _new(_n_)=_abc(_n_)*100;

   end;

proc print;run;

Message was edited by: Linlin

ballardw
Super User

You don't say what you are going to do with a loop but you would likely want to make sure that the variables are the same type for many operations.

Something else to look at might be the Open, Varnum and Varname functions for reading details about datasets but I would likely start with the Dictionary tables i.e. Reeza's sashelp.vcolumns

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 928 views
  • 6 likes
  • 4 in conversation