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

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

8 REPLIES 8
FreelanceReinh
Jade | Level 19

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;
...
Crystal_F
Quartz | Level 8
Thank you for pointing our my type. But it seems to have other problem with the loop.
Crystal_F
Quartz | Level 8
Edit 2 is a good point. Sorry I didn't pay attention to this before. Thanks! I'll give it a try!
Astounding
PROC Star

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.

Crystal_F
Quartz | Level 8
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.
ballardw
Super User

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.

Crystal_F
Quartz | Level 8
Now I got your point! It works! Thank you for your help!
Crystal_F
Quartz | Level 8
Thank you for the cool explanation! Now my problem is fixed with all people's help here. Thanks a lot.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 8 replies
  • 877 views
  • 0 likes
  • 4 in conversation