Sorting by a predefined variable list in a macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Sorting by a predefined variable list in a macro

Hi there,

I am trying to simply some SAS code and in order to make later revisions easier I want to create a list of variables so that if I have to make changes I only need to do it one time. To be more concrete I have to sort a quite a few datasets and for now it is unclear to me if ill end up having to sort by 3 or 4 variables and so it would be a lot easier if I didn’t have to change it 20 places. At the moment I sort: BY pnr senr dto; But what I would like is BY varlist; where varlist is defined at the top of the program even before the macro starts as it will be relevant for several macros along the way.

Cheers,
JD

Here is a small snip of the whole thing:


%macro IBM;
%do yr=8 %to 9;                            

    

   *comment;
DATA ibm_w1_200&yr;
SET ibm.ibm0&yr._dst;
dto=input(periode,yymmn6.);


     *comment;
PROC SORT data=ibm_w1_200&yr;
BY pnr senr dto;


    *comment;

PROC MEANS data=ibm_w1_200&yr NOPRINT;
VAR felt_200;
OUTPUT OUT=data.ibm200&yr (drop=_type_ _freq_)
SUM(felt_200)=felt_200;
BY pnr senr dto;


%END;
%MEND IBM;
%IBM;
RUN;


Accepted Solutions
Solution
‎09-04-2012 07:46 PM
PROC Star
Posts: 7,468

Re: Sorting by a predefined variable list in a macro

umich: you didn't show either the code you tried or your log.  I was suggesting something like (which does work):

%macro ibm(varlist);

  proc sort data=sashelp.class out=test;

    by &varlist.;

  run;

%mend ibm;

%ibm(sex age)

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: Sorting by a predefined variable list in a macro

try:

%let varlist=pnr senr dto;

%macro IBM;
%do yr=8 %to 9;                           

   

   *comment;
DATA ibm_w1_200&yr;
SET ibm.ibm0&yr._dst;
dto=input(periode,yymmn6.);


     *comment;
PROC SORT data=ibm_w1_200&yr;
BY&varlist;


    *comment;

PROC MEANS data=ibm_w1_200&yr NOPRINT;
VAR felt_200;
OUTPUT OUT=data.ibm200&yr (drop=_type_ _freq_)
SUM(felt_200)=felt_200;
BY &varlist;

run;


%END;
%MEND IBM;
%IBM

PROC Star
Posts: 7,468

Re: Sorting by a predefined variable list in a macro

Along the lines that Linlin suggested, couldn't you just pass that into the macro and then just specify your varlist when you run the macro?  e.g. (untested):

%macro IBM (varlist);
%do yr=8 %to 9;                          

  

   *comment;
DATA ibm_w1_200&yr;
SET ibm.ibm0&yr._dst;
dto=input(periode,yymmn6.);


     *comment;
PROC SORT data=ibm_w1_200&yr;
BY&varlist;


etc., etc.


Occasional Contributor
Posts: 9

Re: Sorting by a predefined variable list in a macro

Hi,

Many thanks for your suggestions. If I alter the code to Arthurs example I get the following error message in the log:

No BY statement used or no BY variable specified...

Any idea what needs to be changed?

JD

Solution
‎09-04-2012 07:46 PM
PROC Star
Posts: 7,468

Re: Sorting by a predefined variable list in a macro

umich: you didn't show either the code you tried or your log.  I was suggesting something like (which does work):

%macro ibm(varlist);

  proc sort data=sashelp.class out=test;

    by &varlist.;

  run;

%mend ibm;

%ibm(sex age)

Occasional Contributor
Posts: 9

Re: Sorting by a predefined variable list in a macro

Thanks again Arthur, sorry for not posting the code:


Here is what i wrote:

%let varlist=pnr senr dto;

%macro IBM(varlist);
%do yr=8 %to 9;                           

   

   *comment;
DATA ibm_w1_200&yr;
SET ibm.ibm0&yr._dst;
dto=input(periode,yymmn6.);


     *comment;
PROC SORT data=ibm_w1_200&yr;
BY &varlist;


    *comment;

PROC MEANS data=ibm_w1_200&yr NOPRINT;
VAR felt_200;
OUTPUT OUT=data.ibm200&yr (drop=_type_ _freq_)
SUM(felt_200)=felt_200;
BY &varlist;


%END;
%MEND IBM;
%IBM (varlist);
RUN;


My error it seems was the second last line %IBM (varlist) which needs to %IBM(pnr senr dto) but can that no be avoid ? Because that would still require 6-7 or find and replace instead of just changing the varlist?


Many thanks,

JD



Occasional Contributor
Posts: 9

Re: Sorting by a predefined variable list in a macro

Oh it can be replaced with &varlist.

Thank you so much,

Jd

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 298 views
  • 0 likes
  • 3 in conversation