Hello,
I have a program that I run manually for different years. The sas data table for almost all are in one directory structure and then I have another directory structure for some of the other years.
Ex.
2000-2005 C:\Data\Checking\SAS\Proj1
2006-2009 C:\Data\Checking\SAS\Proj2
The table I want is in that path for that specific year. The issue I have is that one column is different between the two locations.
I have State in the first path and ST in the tables in the second path.
This is what I have tried:
%let year=2009 ;
%macro path ;
%if &year. < 2006 %then %let vName = state ;
%else %let vName = st ;
%mend path ;
%path ;
%put &vName. ;
I don't want to manually change these field names in my selects statements. What would be the best way to define a macro variable that will hold either State or ST and use that in my select statements throughout my program?
My example above is giving me "WARNING: Apparent symbolic reference VNAME not resolved." on %put &vName. ;
proc sql ;
select &vname.
from tblTest
quit ;
Hi @jerry898969 That is because your vName is a local macro variable. Globalise it like -->
%let year=2009 ;
%macro path ;
%global vName;
%if &year. < 2006 %then %let vName = state ;
%else %let vName = st ;
%mend path ;
%path ;
%put &vName. ;
@jerry898969 wrote:
My example above is giving me "WARNING: Apparent symbolic reference VNAME not resolved." on %put &vName. ;
proc sql ;
select &vname.
from tblTest
quit ;
Try this. This code would conditionally check the year and determines the libname based on the year range and renames the field ST to State only if the year is between 2006 and 2009.
Libname Path1 "C:\Data\Checking\SAS\Proj1";
Libname Path2 "C:\Data\Checking\SAS\Proj2";
%let year=2009;
%macro resolve_dates;
%if %eval(&year >= 2000 ) && %eval( &year <= 2005) %then
%do;
Data want;
set path1.have;
run;
%end;
%else %if %eval( &year>=2006) && %eval(&year <= 2009) %then
%do;
Data want;
set path2.have(rename=(ST=state));
run;
%end;
%mend;
%resolve_dates
Hi @jerry898969 That is because your vName is a local macro variable. Globalise it like -->
%let year=2009 ;
%macro path ;
%global vName;
%if &year. < 2006 %then %let vName = state ;
%else %let vName = st ;
%mend path ;
%path ;
%put &vName. ;
@jerry898969 wrote:
My example above is giving me "WARNING: Apparent symbolic reference VNAME not resolved." on %put &vName. ;
proc sql ;
select &vname.
from tblTest
quit ;
Thank you everyone for your replies.
novinosrin,
This worked exactly like i needed it too. Thank you.
Try this..
%let year=2009 ;
%macro path ;
%let vName = sex ;
%if %eval(&year.<2006) %then %let vName = name ;
proc sql ;
select &vname.
from sashelp.class
quit ;
%mend path ;
%path ;
Changes made are the following:
1. Using %eval
2. Moved the proc step inside the macro (if you want to skip the %global macro variable declaration)
3. When year is different then we change the value to be used for macro variable vName. For this case, I can give macro variable a default value. The value will change when your %if condition has been satisfied.
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.