Hi I've a simple SQL work to read in several data sets and keep selective variables from the data, so I used macro. Since the variable list is long and etc. I thought I'd spell out using %let before the macro. When calling in the macro %get, it apparently doesn't recognize these parameters. What do I do, make them system variables? Hope it makes sense. Thanks in advance.
%let yr=2017;
%let yr2=2018;
%let DB1var=v1 v2 v3 v4;
%let DB2var=x5 x7 x14 x5;
%let DB1name=mylib1.dt_A&fy; *mylib1.dt_A2017;
%let DB2name=mylib2.dt_B&fy2; *mylib2.dt_B2018;
%macro get (db=, var=, dbname=);
proc sql; create table &db as select a.*, b.*
from have (where= (&db=1) ) as a
left join &&dbname (keep=&dbvav) as b
on a.ID=b/OD; quit;
%mend;
%get (db=DB1, var=DB1var, dbname= DB1name) ) ;
%get (db=DB2, var=DB2var, dbname= DB1name) ) ;
/*It is how it should be executed if I'm doing it right*/
proc sql; create table DB1 as select b.*
from have (where= (DB1=1) ) as a
left join DT_A2017 (keep=v1 v2 v3 v4) as b on a.ID=b.ID;
quit;
/*What it actually turns out*/
proc sql; create table DB1 as select b.*
from have (where= (DB1=1) ) as a
left join DB1name (keep=DB1var) as b on a.ID=b.ID;
quit;
Take a look at my changes. I have not tested it, but eyeballing it I think it should work.
%let yr=2017;
%let yr2=2018;
%let DB1var=v1 v2 v3 v4;
%let DB2var=x5 x7 x14 x5;
%let DB1name=mylib1.dt_A&yr; *mylib1.dt_A2017; /* Changed &fy to &yr */
%let DB2name=mylib2.dt_B&yr2; *mylib2.dt_B2018; /* Changed &fy2 to &yr2 */
%macro get (db=, var=, dbname=);
proc sql; create table &db as select a.*, b.*
from have (where= (&db=1) ) as a
left join &dbname (keep=ID &var) as b /* Added ID to the keep statement, Removed & from &&dbname, changed &dbvav to &var */
on a.ID=b.ID; quit; /* Changed b/OD to b.ID (I think that was a typo) */
%mend;
%get (db=DB1, var=&DB1var, dbname= &DB1name) ; /* Removed extra close parenthesis, Added & to value in var and dbName */
%get (db=DB2, var=&DB2var, dbname= &DB2name) ; /* Removed extra close parenthesis, Added & to value in var and dbName, changed DB1name to DB2name */
Take a look at my changes. I have not tested it, but eyeballing it I think it should work.
%let yr=2017;
%let yr2=2018;
%let DB1var=v1 v2 v3 v4;
%let DB2var=x5 x7 x14 x5;
%let DB1name=mylib1.dt_A&yr; *mylib1.dt_A2017; /* Changed &fy to &yr */
%let DB2name=mylib2.dt_B&yr2; *mylib2.dt_B2018; /* Changed &fy2 to &yr2 */
%macro get (db=, var=, dbname=);
proc sql; create table &db as select a.*, b.*
from have (where= (&db=1) ) as a
left join &dbname (keep=ID &var) as b /* Added ID to the keep statement, Removed & from &&dbname, changed &dbvav to &var */
on a.ID=b.ID; quit; /* Changed b/OD to b.ID (I think that was a typo) */
%mend;
%get (db=DB1, var=&DB1var, dbname= &DB1name) ; /* Removed extra close parenthesis, Added & to value in var and dbName */
%get (db=DB2, var=&DB2var, dbname= &DB2name) ; /* Removed extra close parenthesis, Added & to value in var and dbName, changed DB1name to DB2name */
@Urban_Science wrote:
Take a look at my changes. I have not tested it, but eyeballing it I think it should work.
%let yr=2017; %let yr2=2018; %let DB1var=v1 v2 v3 v4; %let DB2var=x5 x7 x14 x5; %let DB1name=mylib1.dt_A&fy; *mylib1.dt_A2017; %let DB2name=mylib2.dt_B&fy2; *mylib2.dt_B2018;
I think that you may want start by addressing the year component of these variables. I don't see a definition for the macro variables FY or FY2.
Thanks. I worked. I thought I tried it and it didn't work. But apparently it works well. Again thanks.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.