DATA Step, Macro, Functions and more

Macro function within a data step?

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

Macro function within a data step?

So I have to do the exact same thing over and over again for multiple dataset, so I figured a macro might be helpful. This is the code I am trying to run.

%macro lab (fich, num);

data &fich;

set &fich;

if CRF&num=" " then CRF&num="MISSING";

%let &num=%eval(&num+1);

if CRF&num=" " then CRF&num="MISSING";

run;

%mend lab;

%lab(temp,55);

%lab(temp1,60);

So basically, I have variables (CRF55, CRF56, CRF57..and so on) that I want it to insert the word "MISSING" when the character value is blank.  And then the same thing again for another dataset starting with var CRF60 (so this case will be CRF60, CRF61, CRF62 and so on). I have to do this for 4 varaibles per dataset and all in numerical order (e.g 55,56,57,58 and then another dataset for 60,61,62,63...etc.)

Can someone suggests an easy way to do this without manually coding every dataset?

The only other solution I can think of is create multiple datasteps and do the %let statement to change the num inbetween each datasets. but I feel there is a more efficient way to code this.

%macro lab (fich, num);

data &fich;

set &fich;

if CRF&num=" " then CRF&num="MISSING";

run;

%let &num=%eval(&num+1);

data &fich;

set &fich;

if CRF&num=" " then CRF&num="MISSING";

run;

%mend lab;

Thank you!!!


Accepted Solutions
Solution
‎05-27-2015 11:11 AM
Super User
Super User
Posts: 7,392

Re: Macro function within a data step?

Your better off looking at arrays:

data want;

     set have;

     array crf{*} _character_;

     do i=1 to dim(crf);

          if crf{i}="" then crf{i}="Missing";

     end

run;

View solution in original post


All Replies
Solution
‎05-27-2015 11:11 AM
Super User
Super User
Posts: 7,392

Re: Macro function within a data step?

Your better off looking at arrays:

data want;

     set have;

     array crf{*} _character_;

     do i=1 to dim(crf);

          if crf{i}="" then crf{i}="Missing";

     end

run;

Super User
Posts: 10,483

Re: Macro function within a data step?

To add to RW9 you can also list specific variables instead of all character variables.

array crf{*} crf55 crf56 crf57;

or a variable list

array crf{*} crf55 - crf59;

Super User
Super User
Posts: 6,499

Re: Macro function within a data step?

Why not just use a format instead of modifying the data?

proc format ; value $miss ' '='Missing'; run;

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 211 views
  • 3 likes
  • 4 in conversation