Apologies for double-posting; however, I don't think I was clear enough in my original post--there is more nuance to my problem, I think.
My first issue, is that I am trying establish libnames for a series of folders. I would like to do this in a loop and using a macro. The macro is year = 2020, but some of the year indications in the filepaths are the full four-digit year YYYY and others are the two-digit year YY. This is an established program and it is best to only use the existing macros. (I do realize it would be easiest to create a macro equal to the two-digit year.) What I have is what is below, but I'd like to condense it:
%let lastyear = 2020; libname data11"S:\Projects\Data\2011\Intermediate"; libname data12"S:\Projects\Data\2012\Intermediate"; libname data13"S:\Projects\Data\2013\Intermediate"; libname data14"S:\Projects\Data\2014\Intermediate"; libname data15"S:\Projects\Data\2015\Intermediate"; libname data16"S:\Projects\Data\2016\Intermediate"; libname data17"S:\Projects\Data\2017\Intermediate"; libname data18"S:\Projects\Data\2018\Intermediate"; libname data19"S:\Projects\Data\2019\Intermediate"; libname data20"S:\Projects\Data\2020\Intermediate";
Any thoughts?
My second goal is to improve the efficiency of reading in a set of data sets in a set statement. Again, these have a consistent naming convention and again I am working with a macro that is four digit year of the most recent year of data.
%let lastyear = 2020;
data new_data; set data18.file_name(in=_18) data19.file_name(in=_19) data20.file_name(in=_20); new_var=old_var(mn, dy, yr); run;
@raivester wrote:
Apologies for double-posting; however, I don't think I was clear enough in my original post--there is more nuance to my problem, I think.
My first issue, is that I am trying establish libnames for a series of folders. I would like to do this in a loop and using a macro. The macro is year = 2020, but some of the year indications in the filepaths are the full four-digit year YYYY and others are the two-digit year YY. This is an established program and it is best to only use the existing macros. (I do realize it would be easiest to create a macro equal to the two-digit year.) What I have is what is below, but I'd like to condense it:
%let lastyear = 2020; libname data11"S:\Projects\Data\2011\Intermediate"; libname data12"S:\Projects\Data\2012\Intermediate"; libname data13"S:\Projects\Data\2013\Intermediate"; libname data14"S:\Projects\Data\2014\Intermediate"; libname data15"S:\Projects\Data\2015\Intermediate"; libname data16"S:\Projects\Data\2016\Intermediate"; libname data17"S:\Projects\Data\2017\Intermediate"; libname data18"S:\Projects\Data\2018\Intermediate"; libname data19"S:\Projects\Data\2019\Intermediate"; libname data20"S:\Projects\Data\2020\Intermediate";
Any thoughts?
My second goal is to improve the efficiency of reading in a set of data sets in a set statement. Again, these have a consistent naming convention and again I am working with a macro that is four digit year of the most recent year of data.
%let lastyear = 2020;
data new_data; set data18.file_name(in=_18) data19.file_name(in=_19) data20.file_name(in=_20); new_var=old_var(mn, dy, yr); run;
There is a data step function that will create libraries imaginatively named LIBNAME.
Basically called such as
data _null_; rc= libname('mylib','path'); run;
RC is a return code you could test to see if the assignment is successful. If the second parameter is blank or missing then SAS will clear the libname assignmen.
So if you can create a character variable with the libref and path you want you place the variables in the function:
data _null_; length libref $ 8; do yr=2011 to 2020; libref=cats('data',yr); path= cats('S:\Projects\Data\',yr,'\Intermediate'); rc = libname(libref,path); end; run;
You really do not save anything by using a 2-digit year in your libref but you can play with the above if you really must.
Your example for set statement does not provide any rule for when the list would start. In the latest versions of SAS something like this using a macro %do loop should work:
data new_data; set %do year=2018 %to &lastyear.; data&year..file_name(in=_&year.) %end; ; /*end the SET statement*/ new_var=old_var(mn, dy, yr); run;
Note that the macro processor uses a . to indicate the end of the variable so when you need a . for the libname.dsname you need to provide an extra or it will get eaten.
Again I see no advantage to removing 2 characters in the libname or the IN variables, especially since you are providing "lastyear" in 4 digit form.
You can use the SET option INDSNAME:
%let lastyear = 2020;
data new_data;
set data18.file_name
data19.file_name
data20.file_name
indsname=_dsname;
dsname=_dsname;*optional. Do this or else it will not be added to the output;
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.