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

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 

1 ACCEPTED SOLUTION

Accepted Solutions
lmyers2
Obsidian | Level 7

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?

 

View solution in original post

5 REPLIES 5
Reeza
Super User

What are you trying to do here? It's not clear what you want as output.

Reeza
Super User

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 


 

lmyers2
Obsidian | Level 7

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?

 

Reeza
Super User

 

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?

 


 

lmyers2
Obsidian | Level 7
You're right. I don't need the do loop. I just did a data step and searched within the array and it was much easier!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 5 replies
  • 921 views
  • 0 likes
  • 2 in conversation