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
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.
Ok, thanks a lot for the suggestions. we will do.
"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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.