Using SAS 9.4
I am running the following array (which is insufficient)
data check;
set postop_comps;
format postop_comp_timeframe $10.;
array tots(20) TT_dvt_dx TT_embolism_dx TT_transfusion_dx TT_myocardial_dx TT_cerebro_dx TT_septicemia_dx TT_pneumonia_dx TT_death_dx
TT_other_med_dx /*TT_nerve_dx*/ TT_nerve_dos /*TT_vessel_dx*/ TT_vessel_dos /*TT_retear_dx*/ TT_retear_dos /*TT_laxity_dx*/
TT_laxity_dos /*TT_infect_dehiscence_dx*/ TT_infect_dehiscence_dos /*TT_deep_infect_dx*/ TT_deep_infect_dos /*TT_hardware_dx*/
TT_hardware_dos /*TT_ho_dx*/ TT_ho_dos /*TT_arthrofib_dx*/ TT_arthrofib_dos /*TT_crps_dx*/ TT_crps_dos /*TT_other_surg_dx*/
TT_other_surg_dos;
do i = 1 to 20;
if . < tots(i) < 3 then postop_comp_timeframe = '<3 days';
else if 3<= tots(i) <= 30 then postop_comp_timeframe = '3-30 days';
else if 30< tots(i) <= 90 then postop_comp_timeframe = '30-90 days';
else if tots(i) > 90 then postop_comp_timeframe = '>90 days';
end;
drop i;
run;
Is it possible to update this array in such a way that each new variable 'TT_x' populates to a new created variable 'postop_comps_timeframe1...x'?
Meaning I would have postop_comps_timeframe1 - postop_comps_timeframe20 and each time 'TT_' variable was not blank it would populate into a new ' postop_comps_timeframe'
Thank you for any thoughts!
You can define an array using a list such as:
array t TT_: ;
Note the colon. That tells SAS to use ALL of the variables whose names start with TT_.
Caveat: All of the variables must be of the same type: numeric or character. Also, the order of the variables may be an issue and if you add variables then what was element 5 in one data step may not be element 5 in a later version.
Note that declaring the size of the array is not needed.
To process all elements of the array you would use the function DIM to get the element count:
do i = 1 to dim(t);
<code>
end;
Your current postop_comp_timeframe variable would only hold the value based on the last element of the array processed. If you want to have a postop_comp_timeframe value associated with each TT_ variable then you need another array to hold those values (or do something else).
But that entire do loop may not be needed at all. If you create a custom format such as
proc format library=work; value postop_comp_timeframe 0 - < 3 = '<3 days' 3 - 30 = '3-30 days' 30< - 90 = '30-90 days' 90 <- high = '>90 days' ; run;
And use the format when ever you need that display value:
data example; do i= 0 to 95; put i= +1 "post_comp_timeframe= " i postop_comp_timeframe.; output; end; run; proc freq data=example; tables i; format i postop_comp_timeframe.; run;
The proc freq example is to show that the format will create groups for analysis without adding variables.
So when you have a new variable you just use the format. And a format statement will take a list like:
Format tt_: postop_comp_timeframe. ;
to assign the same format to a bunch of similar variables easily.
A format is often a better approach if a value is needed based on a single variable. Also the sort order will work as you may expect it. Plus you could have multiple formats to do different ranges without having to add more variables to hold the formatted value and don't have to worry much about the length of the variable place the value into. The format holds the needed length.
You can define an array using a list such as:
array t TT_: ;
Note the colon. That tells SAS to use ALL of the variables whose names start with TT_.
Caveat: All of the variables must be of the same type: numeric or character. Also, the order of the variables may be an issue and if you add variables then what was element 5 in one data step may not be element 5 in a later version.
Note that declaring the size of the array is not needed.
To process all elements of the array you would use the function DIM to get the element count:
do i = 1 to dim(t);
<code>
end;
Your current postop_comp_timeframe variable would only hold the value based on the last element of the array processed. If you want to have a postop_comp_timeframe value associated with each TT_ variable then you need another array to hold those values (or do something else).
But that entire do loop may not be needed at all. If you create a custom format such as
proc format library=work; value postop_comp_timeframe 0 - < 3 = '<3 days' 3 - 30 = '3-30 days' 30< - 90 = '30-90 days' 90 <- high = '>90 days' ; run;
And use the format when ever you need that display value:
data example; do i= 0 to 95; put i= +1 "post_comp_timeframe= " i postop_comp_timeframe.; output; end; run; proc freq data=example; tables i; format i postop_comp_timeframe.; run;
The proc freq example is to show that the format will create groups for analysis without adding variables.
So when you have a new variable you just use the format. And a format statement will take a list like:
Format tt_: postop_comp_timeframe. ;
to assign the same format to a bunch of similar variables easily.
A format is often a better approach if a value is needed based on a single variable. Also the sort order will work as you may expect it. Plus you could have multiple formats to do different ranges without having to add more variables to hold the formatted value and don't have to worry much about the length of the variable place the value into. The format holds the needed length.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.