BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DBAgFinance
Fluorite | Level 6

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

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;

ballardw
Super User

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.

 

rogerjdeangelis
Barite | Level 11

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

 

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2104 views
  • 1 like
  • 4 in conversation