DATA Step, Macro, Functions and more

Macro: looping through columns

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Macro: looping through columns

Hi,

I'm a bit of a Macro noob, and was wondering whether someone could help me tackle this problem.

What I have is an array where individual data is organized by columns, e.g.

Obs       a1          a2          b1          b2     etc

1          200         300         250        300

2          220         280         280        320

3          210         280         270        300

.

.

100      210          310         260        300

The names of the columns do not ascend in any particular order. I would like to cycle through each of the columns and perform some macro functions on each, and I'm not quite sure how to go about it. It would make sense to me to use a do loop, but I'm not sure how to retrieve the column names. I have also made another table which is just the column names, so I could iterate through that, but I'm not sure how to retrieve single table elements.

Please help! Greatly appreciated!

Thanks,

Lucy


Accepted Solutions
Solution
‎03-12-2012 02:04 PM
Super User
Super User
Posts: 7,074

Macro: looping through columns

You can get a list of the variable names in a dataset from PROC CONTENTS or by querying SAS's internal metadata tables.

proc contents data=mydata out=contents noprint; run;

proc sql ;

   select name into :varlist separated by ' '

  from contents

   ;

%let n=&sqlobs;

quit;

%do i=1 %to &n ;

    %let currentvar = %scan(&varlist,&i);

....repetitive code....

%end;

View solution in original post


All Replies
Super User
Super User
Posts: 7,074

Macro: looping through columns

I doubt that you want SAS macro logic to do anything with the values in columns of your data.

Usually you use macro code to generate SAS code that can perform calculations on values.

Can you explain what you want to do?  It might even be possible to do it without any macro code.

Occasional Contributor
Posts: 8

Macro: looping through columns

I subsequently do perform a set of functions on the columnar data: cumulative sum, loess procedure on the csum, regression etc. I have written these functions for data from one column, which I specify, and I just want to apply the same functions in the same order to every column in the dataset.

Solution
‎03-12-2012 02:04 PM
Super User
Super User
Posts: 7,074

Macro: looping through columns

You can get a list of the variable names in a dataset from PROC CONTENTS or by querying SAS's internal metadata tables.

proc contents data=mydata out=contents noprint; run;

proc sql ;

   select name into :varlist separated by ' '

  from contents

   ;

%let n=&sqlobs;

quit;

%do i=1 %to &n ;

    %let currentvar = %scan(&varlist,&i);

....repetitive code....

%end;

Super User
Super User
Posts: 7,074

Macro: looping through columns

You can also look into transposing your data and then using BY variable processing.

proc transpose data=mydate out=vertical;

run;

proc means data=vertical;

    by _name_ ;

    var col1;

run;

.....

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 6262 views
  • 0 likes
  • 2 in conversation