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

I've one SQL which I'm currently executing multiple times with different values. For example, assume the SQL is executing with one condition (e.g. emp_id=123) and the next time I'm executing with emp_id=234 and so on to create multiple tables.

 

Can we achieve this task in macro to execute the SQL once by passing different values to create multiple tables? Any pointers to the documents will also helpful.

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

%macro doSQL(id);

    SELECT *
    FROM aDataset
    WHERE emp_id=&id.;

%mend;

proc sql;

    %doSQL(123);

    %doSQL(456);

quit;

Why do you want to create separate datasets per id ? Depending on what you are trying to do, a single

request with a GROUP BY statement could be a better solution.

View solution in original post

3 REPLIES 3
gamotte
Rhodochrosite | Level 12

Hello,

 

%macro doSQL(id);

    SELECT *
    FROM aDataset
    WHERE emp_id=&id.;

%mend;

proc sql;

    %doSQL(123);

    %doSQL(456);

quit;

Why do you want to create separate datasets per id ? Depending on what you are trying to do, a single

request with a GROUP BY statement could be a better solution.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

"to create multiple tables" - really not a good idea, that is not thinking "the SAS way".  Just create grouping variables in the data for each group, and then use those grouping data for further processing.  E.g. you could loop over:

- create dataset with subject data for 001

- print dataset with subject 001 data

- create dataset with subject 002

...

 

Or you could simply say:

- print big dataset by subject where subject in 001,002...

 

As such the question is moot, a change to the thought process removes the need for extra messy maintainable code.

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 3 replies
  • 1548 views
  • 1 like
  • 4 in conversation