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!
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;
...
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;
...
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.
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.