Help using Base SAS procedures

doubt

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

doubt

I have merge step like belw which has to merge jan and feb months data to the main master thereby creating feb dataset.

Likewise in March, the master should be merged with jan,feb and march months data. though i was able to create a macro for reading

the required input files for a particular month correctly (that is for Feb month, jan and feb months data should be read)

but im helpless in automating the below step. Colud anyone help me out .

Thanks in advance.

data feb;

merge master(in=a) jan(in=b) feb(in=c);

by id;

if a;

run;


Accepted Solutions
Solution
‎02-27-2012 09:56 PM
Super User
Posts: 9,662

doubt

%let want_month=4 ; *must be number of month , 4 is April ;

options mprint;
%macro month;
%let list=Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec;
data %scan(&list,&want_month) ;
 merge master(in=a) %do i=1 %to &want_month ;
                       %scan(&list,&i)
                    %end;
        ;
 by id;
 if a;
 run;
%mend month;
%month

Ksharp

View solution in original post


All Replies
PROC Star
Posts: 7,356

doubt

First, do you really want to merge the files, or do you really just want to add new records to the master file?

Second, regardless of whether you really want to merge or concatenate, I think it would be easiest if you either updated the master each month or, if that isn't possible, build a macro that accomplishes the task one month at a time.

New Contributor
Posts: 4

doubt

Thanks for your reply.

Yes i only have to merge the files. Will try building a macro then ..

but other than this is there not any other better way?.

PROC Star
Posts: 7,356

doubt

To answer your question the forum members would need to know more about your data.  It would help if you could post example data for your master and, say, jan and feb, and what you want the resulting file to look like given that example data.

Regular Contributor
Posts: 233

doubt

Can you please let me know how the names appear in your data base. That way it will help us in coding.

Super User
Posts: 17,730

doubt

Since you're only keeping records in the master data set anyways (if a) then perhaps look at the update and modify options instead of a merge?

It depends on what your'e looking to do, so need more details as others have indicated.

Regular Contributor
Posts: 233

doubt

Assuming that the table MASTER_FINAL is a data set that has January's data merged with your file. The table for this month is TABLE_1202.

%MACRO RUNTHIS(TABLEDT);
DATA MASTER;
MERGE MASTER_FINAL (IN=A) TABLE_&TABLEDT. (IN=B);
BY ID;
IF A;
RUN;

DATA MASTER_FINAL;

SET MASTER;

RUN;
%MEND;

%RUNTHIS(1202)

New Contributor
Posts: 4

doubt

Thanks Hima for yur reply

Solution
‎02-27-2012 09:56 PM
Super User
Posts: 9,662

doubt

%let want_month=4 ; *must be number of month , 4 is April ;

options mprint;
%macro month;
%let list=Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec;
data %scan(&list,&want_month) ;
 merge master(in=a) %do i=1 %to &want_month ;
                       %scan(&list,&i)
                    %end;
        ;
 by id;
 if a;
 run;
%mend month;
%month

Ksharp

New Contributor
Posts: 4

doubt

Oh Great!!....Your code is really interesting ..Never thought that we can do iterations on a dataset mentioned within a merge step...!! Learnt sumthning new ..

Thanks alot Ksharp.Smiley Happy

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 226 views
  • 0 likes
  • 5 in conversation