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.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1345 views
  • 1 like
  • 4 in conversation