BookmarkSubscribeRSS Feed
rainemationg03
Calcite | Level 5

Hi everyone, 

 

I was wondering if anyone can help me on how to code using macro.I am very new to this.

 

I have three survey data sets, collected at baseline, follow-up1 and follow up 2. These datasets have similar information collected. In order not to repeat the same codes in processing the data from one dataset to another, I am planning to use macro codes.  I am using SAS 9.4 for windows system.

 

%let MainPath= C:\MGP;

 

*Exctracting datasets from Access database and Assigning Libnames;

 

/*baseline*/
libname mgpbl "&MainPath\Data\Source Data\BL\UB2016BL.mdb";

 

/*FU1*/
libname mgpf1 "&MainPath\Data\Source Data\FU1\UB2016FU1.mdb";

 

/*FU2*/
libname mgpf2 "&MainPath\Data\Source Data\FU2\UB016FU2.mdb";


/*libname SAS data*/

libname mgp "&MainPath\Data\SASData";

 

 

I'm already stucked in the following because I am getting errors, I don't know how to proceed:

 

%macro St (TimeIn = ,  mgpbl = mgpbl);

data mgp.&TimeIn.StResults
set &mgpbl..&TimeIn.StColl 
%if &TimeIn eq Baseline %then %do; 

%mend

 

Hope  you can shed some light. 

 

Thanks

4 REPLIES 4
andreas_lds
Jade | Level 19

Do you have working code solving the issue for one dataset? If not: write that code, then identifying the things that have to be replaced by macro-code are more obvious.

 

In the posted code, an %end-statement is required before %mend.

rainemationg03
Calcite | Level 5

Ok, thanks a lot for the suggestions. we will do. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

"I have three survey data sets, collected at baseline, follow-up1 and follow up 2. These datasets have similar information collected"

If you have data that is the same, put it in one dataset, then your code only has to execute, and produce output once, rather than 3 times which is the way your aiming.  For instance:

/*baseline*/
libname mgpbl "&MainPath\Data\Source Data\BL\UB2016BL.mdb";

/*FU1*/
libname mgpf1 "&MainPath\Data\Source Data\FU1\UB2016FU1.mdb";

/*FU2*/
libname mgpf2 "&MainPath\Data\Source Data\FU2\UB016FU2.mdb";

data total;
  length source $200;
  set mgpbl.<data>    
        mgpf1.<data>
        mgpf2.<data>
        indsname=tmp;
  source=tmp;
run;

Note that I use <data> as I do not know what your sets are called.  What this will do is put all the data from the three tables one under the other, and have a variable name as the source.  Then you can process as one dataset.  If you need them horizontal, you could merge (join) them, or transpose from the one datasource.

Tip: Macro is never needed, use Base SAS all the time to get robust, resource friendly, easy to read code.  Only resort to macro where it adds something to the process, such as re-usability. 

rainemationg03
Calcite | Level 5
Thank you so much for your reply. We will try the codes that you have suggested.

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!

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
  • 4 replies
  • 807 views
  • 0 likes
  • 3 in conversation