%LET StartYr=2010;
%LET EVENTYr=2023;
%MACRO TESTyr(EventYr, StartYr);
%do year = &StartYr %to &EventYr;
data .years2;
%let drop_vars = A&year._%eval(&year-2); /******** Variable names are A2008_2006, A2009_2007........ A2023_2021 ***************/
data .years2;
set DATASET (drop=%drop_vars); %end;
dyear = &year; /***** other NEW variable, DYEAR in dataset *********/
run;
%mend;
%TestYr(&startyr, %eventyr);
Hi, set dataset (drop A2008_2006) WORKS WELL, but how do I drop multiple columns in macro variable list?
Thanks.
Either put ALL of the variable names into VARLIST.
%macro testyr(eventyr, startyr);
%local year varlist;
%do year = &startyr %to &eventyr;
%let varlist=&varlist a&year._%eval(&year-2);
%end;
data years2;
set dataset;
drop &varlist;
run;
%mend testyr;
%testyr(startyr=2010,eventyr=2023)
Or move the %DO loop into the middle of the DROP statement.
%macro testyr(eventyr, startyr);
%local year ;
data years2;
set dataset;
drop
%do year = &startyr %to &eventyr;
a&year._%eval(&year-2)
%end;
;
run;
%mend;
%testyr(startyr=2010,eventyr=2023)
But this statement in your original data step is a problem:
dyear = &year;
The macro variable YEAR will have many different values, from STARTYR to EVENTYR. Which one to you want to store into the dataset variable DYEAR?
This code shows one way to place all the names into a single variable:
%LET StartYr=2010; %LET EVENTYr=2023; %let varlist=; %macro testyr(EventYr, StartYr); %do year = &StartYr %to &EventYr; %let varlist.= &varlist. A&year._%eval(&year.-2); %end; %put &varlist; %mend; %testyr(&eventyr. , &startyr.);
Use &varlist. anywhere following the definition where you want a space-delimited list of variable names. such as (drop=&varlist. ) as a data set option. (NOT %varlist)
@stella7 wrote:
%LET StartYr=2010;
%LET EVENTYr=2023;
%MACRO TESTyr(EventYr, StartYr);
%do year = &StartYr %to &EventYr;
data .years2;
%let drop_vars = A&year._%eval(&year-2); /******** Variable names are A2008_2006, A2009_2007........ A2023_2021 ***************/
data .years2;
set DATASET (drop=%drop_vars); %end;
dyear = &year; /***** other NEW variable, DYEAR in dataset *********/
run;
%mend;%TestYr(&startyr, %eventyr);
Hi, set dataset (drop A2008_2006) WORKS WELL, but how do I drop multiple columns in macro variable list?
Thanks.
Either put ALL of the variable names into VARLIST.
%macro testyr(eventyr, startyr);
%local year varlist;
%do year = &startyr %to &eventyr;
%let varlist=&varlist a&year._%eval(&year-2);
%end;
data years2;
set dataset;
drop &varlist;
run;
%mend testyr;
%testyr(startyr=2010,eventyr=2023)
Or move the %DO loop into the middle of the DROP statement.
%macro testyr(eventyr, startyr);
%local year ;
data years2;
set dataset;
drop
%do year = &startyr %to &eventyr;
a&year._%eval(&year-2)
%end;
;
run;
%mend;
%testyr(startyr=2010,eventyr=2023)
But this statement in your original data step is a problem:
dyear = &year;
The macro variable YEAR will have many different values, from STARTYR to EVENTYR. Which one to you want to store into the dataset variable DYEAR?
Expecting a variable name after %LET.
ERROR: Symbolic variable name VARLIST. must contain only letters, digits, and underscores.
ERROR: The macro ROLLYR will stop executing. Do I have to change variable name?
@stella7 wrote:
Expecting a variable name after %LET.
ERROR: Symbolic variable name VARLIST. must contain only letters, digits, and underscores.
ERROR: The macro ROLLYR will stop executing. Do I have to change variable name?
Variable names cannot have a period in them. If the period is not there then there is some other invisible character. Just retype the line.
year comes from the other macro dataset test_temp. I can rename from year to yrs.
However, it has the same errors after I changed the.
data testyrs; set test_temp;
%do year = &StartYr %to &EventYr;
if R&year_%eval(&year-2) ne . then year=&year; ====> R&yrs... &yrs ne. then year1=&yrs;
%end;
run;
Saying something has errors but not showing the error is not helpful.
Please, Copy the text from the log including the code that generates the error message(s) along with the errors and post into a text box opened on the forum with the </> icon that appears above the main message window.
The text box prevents the forum software from reformatting pasted text and sets the log apart from the discussion.
When dealing with MACRO code it is a good idea to run the code with OPTIONS MPRINT; set before running the macro so more details of the generated code are shown in the log. Such as
options mprint; %TestYr(&startyr, %eventyr); options nomprint;
Note: Data sets are data sets. There is no such thing as a "macro data set" as used in your phrase "other macro dataset test_temp". The name of a data set may be stored in a macro variable but it is still a data set (or View).
Also, this is the first mention of the data set temp_test. Which makes it doubly important to post the code involved.
@stella7 wrote:
year comes from the other macro dataset test_temp. I can rename from year to yrs.
However, it has the same errors after I changed the.
data testyrs; set test_temp;
%do year = &StartYr %to &EventYr;
if R&year_%eval(&year-2) ne . then year=&year; ====> R&yrs... &yrs ne. then year1=&yrs;
%end;
run;
"macro dataset" What does that mean? Did you mean "other dataset"? What other dataset? You only mentioned one input dataset in your original question.
Before trying to use macro code to generate code it helps to first write out the actual code you want to run. Then you can work on using the macro language to help you generate that code. So what code would you want to run for your example range of years?
You have data (time periods) in structure (variable names).
Transpose your dataset to long, and extract the start and end years into numeric variables. This will make handling your data much easier.
proc transpose data=have out=long (rename=(col1=a));
by /* whatever identifies your individual observations */;
var a:;
run;
data want;
set long;
start = input(scan(substr(_name_,2),1,"_"),4.);
end = input(scan(_name_,2,"_"),4.);
drop _name_;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.