BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ammarhm
Lapis Lazuli | Level 10

Hi

A quick question.

I have several datasets (HR1, HR2, HR3,.....HR 20)

I need to combine a number of these tables using (data..set) according to certain criteria, since this is a repetitive task I chose to build a macro like this

%macro repeat_set(n=);

%do var=1 %to &n;

Data j;

if &var=1 then do;

           set HR&var;

           end;

else if &var gt 1 then do;

           set j HR&var;

    end;

run;

%end;

%mend;

The reason why I am doing this is that the new dataset 'J' dose not exist from the beginning, so while looping, the idea is when the code takes the first HR set (HR1) it will create 'j' using the following:

data j;

set HR1;

run;

and for the consequent sets, like HR2 , it will do

data j;

set j HR2;

run;

and so on

So when I do a

%repeat_set(n=1);


the code executes correctly


BUT, when I do anything larger than 1, ex

%repeat_set(n=5);


The code combines only tables HR2-HR5. It seems as if HR1 contribution to the new set 'j' is "deleted" during the process.

Any suggestions?

Best regards


1 ACCEPTED SOLUTION

Accepted Solutions
ammarhm
Lapis Lazuli | Level 10

The reason is that it is a part of a larger code that I need to actively decide the number of tables to include, and I need to programatically be able to change one value in the code rather than changing it at several places

Anyhow, found the problem myself (missing %), but your code works too of course

Kind regards

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Why not just combine them all in one step?

data want ;

  set hr1-hr20;

run;

ammarhm
Lapis Lazuli | Level 10

The reason is that it is a part of a larger code that I need to actively decide the number of tables to include, and I need to programatically be able to change one value in the code rather than changing it at several places

Anyhow, found the problem myself (missing %), but your code works too of course

Kind regards

Tom
Super User Tom
Super User

How are you "actively" deciding how many tables to include?  Do you mean the parameter N to the macro?

%let n=5 ;

data J;

  set hr1-hr&n;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 2800 views
  • 6 likes
  • 2 in conversation