Can anyone help me read values within a variable from a SAS data set one at a time and use it in a macro variable for processing?
Table:
PRIORITY ELEMENT
high commodity
high date
high state
.
.
.
Using SAS 9.4, I need to pull the first value for ELEMENT and store it as a macro variable and use it for processing. Once the process is complete I need to move to the next value and repeat the process, then the next value, etc. until the end of file.
Any help is much appreciated. Thank you!
Here's one approach.
%macro all_elements;
%local i element n_elements;
data _null_;
set elements nobs=_nobs_;
call symputx('n_elements', _nobs_);
stop;
run;
%do i=1 %to &n_elements;
data _null_;
set elements (firstobs=&i obs=&i);
call symputx('element', element);
run;
* Add your processing here;
%end;
%mend all_elements;
Here's one approach.
%macro all_elements;
%local i element n_elements;
data _null_;
set elements nobs=_nobs_;
call symputx('n_elements', _nobs_);
stop;
run;
%do i=1 %to &n_elements;
data _null_;
set elements (firstobs=&i obs=&i);
call symputx('element', element);
run;
* Add your processing here;
%end;
%mend all_elements;
You might want to look at Call Execute. It is one way to pass values from a data set in exactly the manner you describe.
For instance if you have a macro that uses your variables as parameters you could do:
Data _null_;
set have;
call execute("%mymacro(parameter1="||Priority||",parameter2="||element||");");
run;
Which would call the macro Mymacro once for each record in the Have data set, passing the variables as values for the parameters.
Is this what you are tryng to do?
HAVE
data elements;
input
ELEMENT $32.;
cards4;
commodity
date
state
;;;;
run;quit;
WANT
data commodity;
element="commodity";
run;quit;
data date;
element="date";
run;quit;
data state;
element="state";
run;quit;
* SOLUTION;
%symdel element;
data _null_;
set elements;
call symputx('element',element);
rc = dosubl ("
data &element;
length element $32;
element=""&element"";
run;quit;");
;run;quit;
NOTE: The data set WORK.COMMODITY has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
NOTE: The data set WORK.DATE has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
NOTE: The data set WORK.STATE has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
NOTE: There were 3 observations read from the data set WORK.ELEMENTS.
NOTE: DATA statement used (Total process time):
real time 3.32 seconds
cpu time 0.23 seconds
Up to 40 obs from WORK.COMMODITY total obs=1
Obs ELEMENT
1 commodity
Up to 40 obs from WORK.DATE total obs=1
Obs ELEMENT
1 date
Up to 40 obs from WORK.STATE total obs=1
Obs ELEMENT
1 state
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!
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.