program error: how to create variable with different suffix with Macro

Accepted Solution Solved
Reply
Contributor
Posts: 49
Accepted Solution

program error: how to create variable with different suffix with Macro

I have a problem to create new variables with different suffix. I'm hoping there is someone here could help me debug and correct the code.

 

My plan is to generate 36 different categorical variables based on the similar criteria, i.e. therapy type, duration and setting. For example, if a patient had physical therapy in inpatient setting within a year since the index date, then it was coded to subpop_phy_1yr_ip_irf=1;

 

The logic is like below:

 

if subpop=1 and sum_phy_mins_1yr_ip_irf >0 then subpop_phy_1yr_ip_irf=1;

else.....

 

Similarily, if a patient had speech therapy in outpatient setting within 30 day since the index date, then a new variable subpop_ST_30day_op is created and coded to 1.

 

My code is as follows:

 

%macro create_flagvar(therapy, duration, setting);
  %let n1=%sysfunc(countw(&therapy));
  %let n2=%sysfunc(countw(&duration));
  %let n3=%sysfunc(countw(&setting));

  data temp3.catpop1;
    set temp3.catpop;
     %do i=1 %to &n1;
       %let thearpy = %scan(&therapy,&i);
          %do j=1 %to &n2;
             %let duration = %scan(&duration,&j);
			    %do k=1 %to &n3;
                   %let setting = %scan(&setting,&k);
				   if subpop=1 and sum_&therapy._mins_&duration._&setting.>0 then subpop_&therapy._&duration._&setting=1;
                   else if subpop=1 and sum_&therapy._mins_&duration._&setting.=0 then subpop_&therapy._&duration._&setting.=2;
                   else if subpop=2 and sum_&therapy._mins_&duration._&setting.>0 then subpop_&therapy._&duration._&setting.=3;
                   else if subpop=2 and sum_&therapy._mins_&duration._&setting.=0 then subpop_&therapy._&duration._&setting.=4;
	               else if flag_98benes=1 and subpop=3 and sum_&therapy._mins_&duration._&setting.>0 then subpop_&therapy._&duration._&setting.=5;
	               else if flag_98benes=1 and subpop=3 and sum_&therapy._mins_&duration._&setting.=0 then subpop_&therapy._&duration._&setting.=6;
                   else subpop_&therapy._&duration._&setting.=7;
				   %end;
		       %end;
			%end;
	run;
%mend;

%create_flagvar(phy OT ST,1yr 30day 90day,ip_irf op snf hha);

 

The log shows something like below:

 

NOTE: Line generated by the macro variable "SETTING".

1 sum_phy OT ST_mins_1yr_ip_irf

--

388

202

NOTE: Line generated by the macro variable "SETTING".

1 subpop_phy OT ST_1yr_ip_irf

----------

180

NOTE: Line generated by the macro variable "SETTING".

1 sum_phy OT ST_mins_1yr_ip_irf

--

388

202

 

 

Anyone know how to fix the problem? Your inputs are highly appreciated! Thank you!

 

 


Accepted Solutions
Solution
‎04-25-2016 02:31 PM
Trusted Advisor
Posts: 1,118

Re: program error: how to create variable with different suffix with Macro

[ Edited ]
Posted in reply to Crystal_F

Hello @Crystal_F,

 

I think, that's an easy one: Just correct the typo in

%let thearpy = %scan(&therapy,&i);

Edit: ... and avoid overwriting the original values of the macro parameters with the extracted substrings! 

 

Edit 2:

Example:

...
%do i=1 %to &n1; 
  %let ther = %scan(&therapy,&i); 
  %do j=1 %to &n2; 
    %let dur = %scan(&duration,&j); 
    %do k=1 %to &n3; 
      %let sett = %scan(&setting,&k); 
      if subpop=1 and sum_&ther._mins_&dur._&sett.>0 then subpop_&ther._&dur._&sett=1;
...

View solution in original post


All Replies
Solution
‎04-25-2016 02:31 PM
Trusted Advisor
Posts: 1,118

Re: program error: how to create variable with different suffix with Macro

[ Edited ]
Posted in reply to Crystal_F

Hello @Crystal_F,

 

I think, that's an easy one: Just correct the typo in

%let thearpy = %scan(&therapy,&i);

Edit: ... and avoid overwriting the original values of the macro parameters with the extracted substrings! 

 

Edit 2:

Example:

...
%do i=1 %to &n1; 
  %let ther = %scan(&therapy,&i); 
  %do j=1 %to &n2; 
    %let dur = %scan(&duration,&j); 
    %do k=1 %to &n3; 
      %let sett = %scan(&setting,&k); 
      if subpop=1 and sum_&ther._mins_&dur._&sett.>0 then subpop_&ther._&dur._&sett=1;
...
Contributor
Posts: 49

Re: program error: how to create variable with different suffix with Macro

Posted in reply to FreelanceReinhard
Thank you for pointing our my type. But it seems to have other problem with the loop.
Contributor
Posts: 49

Re: program error: how to create variable with different suffix with Macro

Posted in reply to FreelanceReinhard
Edit 2 is a good point. Sorry I didn't pay attention to this before. Thanks! I'll give it a try!
Super User
Posts: 5,516

Re: program error: how to create variable with different suffix with Macro

Posted in reply to Crystal_F

Your plan has a problem.  Look what happens when you execute:

 

%let duration = scan(&duration, &j);

 

This statement replaces &DURATION.  It used to contain a set of words, and now it contains only 1.  So the next time %LET executes, trying to read the second word from &DURATION, there is no second word.  %SCAN returns a blank. 

 

You can switch the %LET statements:

 

%let ther = %scan(&therapy, &i);

%let dur = %scan(&duration, &j);

%let set = %scan(&setting, &k);

 

Then refer to the new, shorter macro variable names in all the IF/THEN statements.

Contributor
Posts: 49

Re: program error: how to create variable with different suffix with Macro

Posted in reply to Astounding
Thanks for sharing your thoughts. I did check with number of words. Duration has 3 words there: 1yr 30day 90day. I guess the problem is due to the incorrect loop. After I correct the typo, the log shows:
NOTE: Variable sum_phy_mins_1yr_ is uninitialized.
NOTE: Variable sum_phy_mins__ is uninitialized.
NOTE: Variable sum__mins__ is uninitialized.
Super User
Posts: 11,343

Re: program error: how to create variable with different suffix with Macro

[ Edited ]
Posted in reply to Crystal_F

Looks like something is processing a blank value for Setting which would be a side effect of

%let setting = scan(&setting,&k)

as after the first pass (k=1) Setting has been set to only have one value- the first scan.

 

Or not initializing the "corrected" other variable you may begusing in creating the Sum_&therapy etc variable. Check spellings.

Contributor
Posts: 49

Re: program error: how to create variable with different suffix with Macro

Posted in reply to Astounding
Now I got your point! It works! Thank you for your help!
Contributor
Posts: 49

Re: program error: how to create variable with different suffix with Macro

Posted in reply to Crystal_F
Thank you for the cool explanation! Now my problem is fixed with all people's help here. Thanks a lot.
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 541 views
  • 0 likes
  • 4 in conversation