DATA Step, Macro, Functions and more

using values of a variable from a SAS data set

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

using values of a variable from a SAS data set

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! 


Accepted Solutions
Solution
‎04-20-2016 12:15 PM
Super User
Posts: 5,083

Re: using values of a variable from a SAS data set

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


All Replies
Solution
‎04-20-2016 12:15 PM
Super User
Posts: 5,083

Re: using values of a variable from a SAS data set

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;

Super User
Posts: 10,500

Re: using values of a variable from a SAS data set

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.

 

Valued Guide
Posts: 505

Re: using values of a variable from a SAS data set

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

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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