DATA Step, Macro, Functions and more

Concatenating 3 datasets

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

Concatenating 3 datasets

[ Edited ]

HI,

 

I am trying to concatenate 3 datasets  (QCdata_1, qcdata_2, qcdata_3) with same structure.

If I try

%MACRO DO_SET;

%DO I = 1 %TO 4;

DATA finalQC;

APPEND Qcdata_&I;

%END;

%MEND DO_SET;

%DO_SET;

 

It is fiving me error for append.

 

If I try SET only the last dataset is retained.

 

Please help.

 

Thanks,

Archana

 


Accepted Solutions
Solution
‎06-28-2016 06:31 AM
Super User
Posts: 7,827

Re: C0ncatenating 3 datasets

Posted in reply to ArchanaSudhir

Do this:

%macro do_set;
data finalqc;
set
%do i = 1 %to 4;
  qcdata_&i
%end;
;
run;
%mend;
%do_set;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎06-28-2016 06:31 AM
Super User
Posts: 7,827

Re: C0ncatenating 3 datasets

Posted in reply to ArchanaSudhir

Do this:

%macro do_set;
data finalqc;
set
%do i = 1 %to 4;
  qcdata_&i
%end;
;
run;
%mend;
%do_set;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 53

Re: C0ncatenating 3 datasets

Posted in reply to KurtBremser

If I try this, I am getting

ERROR 180-322: Statement is not valid or it is used out of proper order

 

Super User
Posts: 7,827

Re: C0ncatenating 3 datasets

Posted in reply to ArchanaSudhir

Post the whole log, please.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 53

Re: C0ncatenating 3 datasets

Posted in reply to KurtBremser

  DATA finalQC;
6905      SET
6906       %DO I = 1 %TO 4;
6907        Qcdata_qc_t&I ;
6908       %END;
6909     RUN;
6910  %MEND DO_SET;
6911
6912  %DO_SET;
MLOGIC(DO_SET):  Beginning execution.
MPRINT(DO_SET):   DATA finalQC;
MLOGIC(DO_SET):  %DO loop beginning; index variable I; start value is 1; stop value is 4; by value is
      1.
SYMBOLGEN:  Macro variable I resolves to 1
MPRINT(DO_SET):   SET Qcdata_qc_t1 ;
MLOGIC(DO_SET):  %DO loop index variable I is now 2; loop will iterate again.
SYMBOLGEN:  Macro variable I resolves to 2
NOTE: Line generated by the macro variable "I".
1            Qcdata_qc_t2
             ---------------------------
             180
MPRINT(DO_SET):   Qcdata_qc_t2 ;
MLOGIC(DO_SET):  %DO loop index variable I is now 3; loop will iterate again.
SYMBOLGEN:  Macro variable I resolves to 3
NOTE: Line generated by the macro variable "I".
1            Qcdata_qc_t3
             ---------------------------
             180
MPRINT(DO_SET):   Qcdata_qc_t3 ;
MLOGIC(DO_SET):  %DO loop index variable I is now 4; loop will iterate again.
SYMBOLGEN:  Macro variable I resolves to 4
NOTE: Line generated by the macro variable "I".
1            Qcdata_qc_t4
             ---------------------------
             180
MPRINT(DO_SET):   Qcdata_qc_t4 ;
MLOGIC(DO_SET):  %DO loop index variable I is now 5; loop will not iterate again.
MPRINT(DO_SET):   RUN;

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.FINALQC may be incomplete.  When this step was stopped there were 0
         observations and 7 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds


MLOGIC(DO_SET):  Ending execution.

Super User
Posts: 7,827

Re: C0ncatenating 3 datasets

Posted in reply to ArchanaSudhir

Look again at my proposed solution, and compare it to your code.

You will find that you added a semicolon (that does not belong there) in your log line 6907.

 

As an exercise in macro programming, try to find out why it must not be there.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 53

Re: C0ncatenating 3 datasets

Posted in reply to KurtBremser

Thanks a lot!

 

I think ; should not be included becasue the format to concatenate datasets is:

 

DATA xyz;

 Set a b c d ;

 

Set, followed by all datase's name folllowed by ';'

 

 

Thanks,

Archana

Super User
Posts: 7,827

Re: C0ncatenating 3 datasets

Posted in reply to ArchanaSudhir

Well done, young padawan!

Smiley Wink

 

This is why I always put macro statements (that need a semicolon to complete) and text that will be created by the macro on separate lines.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 286 views
  • 1 like
  • 2 in conversation