how do i write sas macro to:

Reply
Frequent Contributor
Posts: 102

how do i write sas macro to:

data g1991; set g42; if pfoyr=1991; rename pfo1=pfo1991; drop pfoyr; run; proc sort data=g1991; by cusip; run;

data g1992; set g42; if pfoyr=1992; rename pfo1=pfo1992; drop pfoyr; run; proc sort data=g1992; by cusip; run;

data g1993; set g42; if pfoyr=1993; rename pfo1=pfo1993; drop pfoyr; run; proc sort data=g1993; by cusip; run;

data g1994; set g42; if pfoyr=1994; rename pfo1=pfo1994; drop pfoyr; run; proc sort data=g1994; by cusip; run;

data g1995; set g42; if pfoyr=1995; rename pfo1=pfo1995; drop pfoyr; run; proc sort data=g1995; by cusip; run;

data g1996; set g42; if pfoyr=1996; rename pfo1=pfo1996; drop pfoyr; run; proc sort data=g1996; by cusip; run;

thnk you!

Super User
Super User
Posts: 6,495

Re: how do i write sas macro to:

I wonder if there isn't a better way to do what you really want to do.  But here is one method that takes advantage of the fact that you can write multiple output files in one data step to cut down on the number of times the input file has to be read.

%macro split(in,fyr,lyr);

%local yr;

data

  %do yr=&fyr %to &lyr;

     g&yr (rename=(pfo1=pfo&yr))

  %end;

;

  set ∈

  select (pfoyr);

  %do yr=&fyr %to &lyr;

     when (&yr) output g&yr;

  %end;

     otherwise ;

  end;

run;

%do yr=&fyr %to &lyr;

  proc sort data=g&yr;

     by cuspid;

  run;

%end;

%mend split;



* Make a sample data set test ;

data g42;

  cuspid=1;pfoyr=1991;pfo1=1;

run;

%split(g42,1991,1996);

Super User
Posts: 17,734

Re: how do i write sas macro to:

Why do you want to split the datasets and rename them that way? It looks like a roundabout way to transpose so you may want to look into that directly.

SAS Learning Module: How to reshape data long to wide using proc transpose

Ask a Question
Discussion stats
  • 2 replies
  • 151 views
  • 0 likes
  • 3 in conversation