Hello,
I have a simple question about making binary variables from an array. Sample data and code are below. I believe my code will categorize Case 3 as 'first' and then it doesn't keep reading to SERVICE_EXTENDER2 to also include it as having a 'second.' I tried using "else" in the code but that didn't help. I'm sure there's an easy solution...
Case SERVICE_EXTENDER1 SERVICE_EXTENDER2 SERVICE_EXTENDER3 SERVICE_EXTENDER4
1 first
2 second
3 first second
4 first
data want; set have;
array SERVICE_EXTENDER [4] $ SERVICE_EXTENDER1-SERVICE_EXTENDER4;
do z=1 to 4;
if SERVICE_EXTENDER [z]='first' then new_variable='first';
if SERVICE_EXTENDER [z]='second' then new_variable='second';
end;
run;
proc freq data=want; tables new_variable*case_control / missing; run;
Thanks
laura
Thanks so much for responding. What I'd like is for case 3 to be counted in both new_variable='first' and new_variable='second,' since the array contains both. Is there a way to do this with the array syntax I have, maybe without the do loop?
What are you trying to do here? It's not clear what you want as output.
You likely only need WHICHC.
First_Flag = whichc('first', of service_extender1-service_extender4)>0;
Second_Flag = whichc('second', of service_extender1-service_extender4)>0;
Note that you do not need a loop or array declaration unless you really want to.
@lmyers2 wrote:
Hello,
I have a simple question about making binary variables from an array. Sample data and code are below. I believe my code will categorize Case 3 as 'first' and then it doesn't keep reading to SERVICE_EXTENDER2 to also include it as having a 'second.' I tried using "else" in the code but that didn't help. I'm sure there's an easy solution...
Case SERVICE_EXTENDER1 SERVICE_EXTENDER2 SERVICE_EXTENDER3 SERVICE_EXTENDER4
1 first
2 second
3 first second
4 first
data want; set have; array SERVICE_EXTENDER [4] $ SERVICE_EXTENDER1-SERVICE_EXTENDER4; do z=1 to 4; if SERVICE_EXTENDER [z]='first' then new_variable='first'; if SERVICE_EXTENDER [z]='second' then new_variable='second'; end; run; proc freq data=want; tables new_variable*case_control / missing; run;
Thanks
laura
Thanks so much for responding. What I'd like is for case 3 to be counted in both new_variable='first' and new_variable='second,' since the array contains both. Is there a way to do this with the array syntax I have, maybe without the do loop?
Show your expected output and it'll be clear (to me) what needs to change. Right now, you're creating a single variable that indicates first or second, but your data clearly indicates that both can occur. So what do you want to do when that case is encountered?
@lmyers2 wrote:
Thanks so much for responding. What I'd like is for case 3 to be counted in both new_variable='first' and new_variable='second,' since the array contains both. Is there a way to do this with the array syntax I have, maybe without the do loop?
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!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.