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

I am using the below macro code(using it as i want the same variable name from 2 different dataset).

%macro EOex(inp=,testvar=);
data EO;
   set EOex.&inp.;
   EOTEST=upcase(cats(substr(&testvar,1,4),substr(&testvar,length(&testvar)-1)));
   EOCAT=upcase(scan(&testvar,1));
run;
%mend;
%EOex(inp=EO_incl,testvar=EOi_std);
%EOex(inp=EO_excl,testvar=EOe_std);

But if i use it then it will just give me the last call and not the set of 2 calls. How can i set them in the same program?

 

Any help

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

As I have mentioned on many occasions, macro code does not do anything, it is a text find and replace system.  So what do we do with the code you presented, we do a find and replace, so that code becomes:

data EO;
   set EOex.EO_incl;
   EOTEST=upcase(cats(substr(EOi_std,1,4),substr(EOi_std,length(EOi_std)-1)));
   EOCAT=upcase(scan(EOi_std,1));
run;
data EO;
   set EOex.EO_excl;
   EOTEST=upcase(cats(substr(EOe_std,1,4),substr(EOe_std,length(EOe_std)-1)));
   EOCAT=upcase(scan(EOe_std,1));
run;

Now what part of this actual executable code does not work the way you had intended?  Is it because you are overwriting EO in both the first and second calls to the macro?  

Also, you would again find your like is much simpler using industry standards - possibly CDISC SDTM due to this being inclusion/exclusion information, this way you would have all the data in one dataset, and the same code could run over just one dataset.  Even if not using that, you can simplfy your code 50% by:

data eo;
  set eoex.eo_incl (rename=(eoi_std=std))
        eoex.eo_excl (rename=(eoe_std=std));
  eotest=upcase(cats(substr(std,1,4),substr(std,length(std)-1)));
  eocat=upcase(scan(std,1));
run;

See, never a need to use macro.

View solution in original post

1 REPLY 1
RW9
Diamond | Level 26 RW9
Diamond | Level 26

As I have mentioned on many occasions, macro code does not do anything, it is a text find and replace system.  So what do we do with the code you presented, we do a find and replace, so that code becomes:

data EO;
   set EOex.EO_incl;
   EOTEST=upcase(cats(substr(EOi_std,1,4),substr(EOi_std,length(EOi_std)-1)));
   EOCAT=upcase(scan(EOi_std,1));
run;
data EO;
   set EOex.EO_excl;
   EOTEST=upcase(cats(substr(EOe_std,1,4),substr(EOe_std,length(EOe_std)-1)));
   EOCAT=upcase(scan(EOe_std,1));
run;

Now what part of this actual executable code does not work the way you had intended?  Is it because you are overwriting EO in both the first and second calls to the macro?  

Also, you would again find your like is much simpler using industry standards - possibly CDISC SDTM due to this being inclusion/exclusion information, this way you would have all the data in one dataset, and the same code could run over just one dataset.  Even if not using that, you can simplfy your code 50% by:

data eo;
  set eoex.eo_incl (rename=(eoi_std=std))
        eoex.eo_excl (rename=(eoe_std=std));
  eotest=upcase(cats(substr(std,1,4),substr(std,length(std)-1)));
  eocat=upcase(scan(std,1));
run;

See, never a need to use macro.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 986 views
  • 0 likes
  • 2 in conversation