BookmarkSubscribeRSS Feed
Abishekaa
Obsidian | Level 7

When I run this macro, I get multiple overwritten notes for variables visit and parm in the SAS log. I am confused since these variables are dropped in the data step. How can I change the code so that visit and parm variables are not overwritten anymore?

data test;
    merge test2(in=inA)
		
		  %macro merge(parm=,var=,and=);
             test3(keep=PtID Visit parm value0 
	                  where=(parm="&parm" and visit="Baseline" &and)
			          rename=(value0=&var._0 ))
	         test3(keep=PtID Visit parm value valueChg valueChgPct 
	                  where=(parm="&parm" and visit="48 Month Follow-Up" &and)
			          rename=(value=&var._48 valueChg=&var.Chg valueChgPct=&var.ChgPct))
          %mend;

		  %merge(parm=parm1,var=var1)
		  %merge(parm=parm2,var=var2)
		  %merge(parm=parm3,var=var3)
		  %merge(parm=parm4,var=var4)
		  %merge(parm=parm5,var=var5);
		  
	by Pt;
	if inA;
	drop visit parm;
run;

Multiple Log notes like this:

INFO: The variable visit on data set WORK.TEST3 will be overwritten by data set WORK.TEST3.

INFO: The variable parm on data set WORK.TEST3 will be overwritten by data set WORK.TEST3.

1 REPLY 1
yabwon
Onyx | Level 15

1) variables are dropped but at the "very end" of the process (just for clarification, moving the "drop" statement at the beginning of the data step code will not change that).

2) read this article by Bob Virgille: How MERGE Really Works so you will understand what is happening under the hood.

3)  good programming practice is to keep source code of your macro outside the data step, after all macros suppose to be (among others) "wrappers for reusable code", right?

4) since this works:

data test;
  set sashelp.class(
    rename=(age=abcdef)
    where=(abcdef=12)
  )
  ;
run;

then this modification should too (you didn't provided any test data, so I can test it myself)

%macro merge(parm=,var=,and=);
  %local f s;
  %let f=______A&sysindex.;
  %let s=______B&sysindex.;
  test3(keep=PtID Visit parm value0
  rename=(Visit=V&f. parm=P&f.) 
  where=(P&f.="&parm" and V&f.="Baseline" &and)
  rename=(value0=&var._0 ))
  test3(keep=PtID Visit parm value valueChg valueChgPct
  rename=(Visit=V&s. parm=P&s.)  
  where=(P&s.="&parm" and V&s.="48 Month Follow-Up" &and)
  rename=(value=&var._48 valueChg=&var.Chg valueChgPct=&var.ChgPct))
%mend;

and at the end of the data step:

drop V______: P______: ;

to drop unnecessary variables.

 

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 328 views
  • 1 like
  • 2 in conversation