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

Hi all,

I have a question on combining some data sets with macro. I have 4 tests and 12 grades. I would like to combine all of the grades for each relevant test. I don't get any error with the below code but it is taking only grade 12. Could you help me with what I am missing?

 

Thanks

 

 

%macro combine(k);
data &k._pl_all_n;
%do i=1 %to 12;
set pls_&k._gr_&i. ;
%end;
run;
%mend;
%combine(listening);
%combine(writing);
%combine(speaking);
%combine(reading);

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

You need a SET statement with a list of datasets, not a list of set statements where each overwrites the preceeding:

 

%macro combine(k);
data &k._pl_all_n;
set 
%do i=1 %to 12;
    pls_&k._gr_&i.
    %end;
; 
run;
%mend;
PG

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

You need a SET statement with a list of datasets, not a list of set statements where each overwrites the preceeding:

 

%macro combine(k);
data &k._pl_all_n;
set 
%do i=1 %to 12;
    pls_&k._gr_&i.
    %end;
; 
run;
%mend;
PG
dustychair
Pyrite | Level 9
Thank you so much!
PGStats
Opal | Level 21

Note that you could just as well use a dataset list:

 

%macro combine(k);
data &k._pl_all_n;
set pls_&k._gr_1 - pls_&k._gr_12;
run;
%mend;
PG
Kurt_Bremser
Super User

Your macro creates these statements (SET is a statement, not a function!):

set pls_listening_gr_1;
set pls_listening_gr_2;
set pls_listening_gr_3;
set pls_listening_gr_4;
set pls_listening_gr_4;
set pls_listening_gr_5;
set pls_listening_gr_6;
set pls_listening_gr_7;
set pls_listening_gr_8;
set pls_listening_gr_9;
set pls_listening_gr_10;
set pls_listening_gr_11;
set pls_listening_gr_12;

so the data step reads an observation each from all 12 datasets (overwriting the PDV each time), and only the data from the last read remains to be written to the output dataset.

Move the SET statement out of the macro loop:

%macro combine(k);
data &k._pl_all_n;
set
%do i=1 %to 12;
  pls_&k._gr_&i.
%end;
;
run;
%mend;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 517 views
  • 0 likes
  • 3 in conversation