-
Notifications
You must be signed in to change notification settings - Fork 8
Macro Variable Manipulation Examples
The library "Medical" has the following tables, columns, and values. These are dimensional tables, that is, they have unique rows for each entity.
Clinics
Clinic_ID Clinic_Name
01 Main
02 West
03 East
04 Othertown
Providers
Provider_ID Provider_Name
1001 Dr. John A
1002 Dr. Jim B
1003 Dr. Jane C
1004 Dr. Jessica D
To generate a macro variable with a list of all Clinic_ID values or Provider_ID values:
%IntoList(Clinics, Clinic_ID, ClinicList, sepby=COMMA);
The macro will generate 1 macro variable: ClinicList = 01, 02, 03, 04
%IntoList(Providers, Provider_ID, ProviderList, sepby=QQC);
The macro will generate 1 macro variable: ProviderList = "1001", "1002", "1003", "1004"
Note: These macro variables are good for using in IN lists. Check the macro documentation for additional delimiters (sepby=).
To generate multiple macro variables, one for each clinic or provider:
%ObsMac(Clinics, Clinic_ID, Clinic);
The macro will generate 4 macro variables with the values: Clinic1 = 01 Clinic2 = 02 Clinic3 = 03 Clinic4 = 04
Note: These macro variables are good for using in macro loops that iterate based on macro variable names. For example:
%macro loop;
%local i;
/* Note: ClinicCNT is generated by the ObsMac macro program */
%do i=1 %to &ClinicCNT;
%let current=&&CLINIC&I;
proc sql;
create table Clinic_&CURRENT._Patients as
select *
from patients
where clinic_id="&CURRENT"
;
quit;
%end;
%mend loop;
%loop;
To generate macro variables for each column names in a table:
%SetVars(Clinics, ClinicVars);
The macro will generate 2 macro variables with the values: ClinicVars1 = Clinic_ID ClinicVars2 = Clinic_Name
%SetVars(Clinics, ClinicVars, type=LIST);
The macro will generate 1 macro variable: ClinicsVars = Clinic_ID Clinic_Name
Note: The type is set to MULTI by default; thus, it is not necessary to specify it in the first SetVars example.
To set the tables themselves to macro variables:
%TableVars(Medical, table);
The macro will generate 2 macro variables with the values:
table1 = Clinics table2 = Providers
Note: This is also good for looping (see below). This macro is essentially a copy of ObsMac with a fixed data source (SASHELP.VTABLE).
%macro loop;
%local i;
/* Note: TABLECNT is generated by the TableVars macro program */
%do i=1 %to &TABLECNT;
%let current=&&TABLE&I;
data _temp_;
set &CURRENT;
run;
/* Additional code... */
%end;
%mend loop;
%loop;
To create macro variables with their names based on values in a column and the macro variable values based on values in another column:
%VarMac(Clinics, Clinic_Name, Clinic_ID);
The macro will generate 4 macro variables with the values: Main = 01 West = 02 East = 03 Othertown = 04
Note: This can be useful to convert one type of identification to another. It is also useful to specify the source for a query and still be able to change it if necessary (e.g., a macro variable named "source" with the value "table_a").
Disclaimer: The SAS macros on this site are provided "as-is". The user is responsible for testing the code on their platform. The user agrees that the author will not, under any circumstances, be held accountable for any damages of any type that result from using these macros. Please review the license provided with the programs.
- Home
- Categories
- Documentation
- Send Feedback [External]
- Christopher A. Swenson
- About & Contact [External]
- Professional Samples [External]
- Resume [External]
- References [External]
- Blog [External]