Using SAS EG 7.15 HF9 (7.100.5.6226) (64-bit) on a Windows 10 computer.
I have 12 variables that are each Yes/No. Each variable represents a job a person reported to have. They can report more than one job. What I need to do is loop through all 12 Y/N variables and determine which ones have "Yes" values. Then, for the "Yes" jobs I need to pull on each one's name and address fields and populate that in new variables for up to 3 jobs, two fields each: WORKNAME1, WORKADD1, WORKNAME2, WORKADD2, WORKNAME3,WORKADD3. To add more complexity since a person can report up to 12 jobs but we can only accept three, there is a hierarchy to determine which to bring in e.g. If they reported to have jobs in retail, government, healthcare, and teaching I should only take the teaching, healthcare and government jobs.
I've racked my brain and I can't think of an efficient solution to this. I'm sure I should use some sort of looping method but I've never done a loop like this. Any help is appreciate. Thank you!
Data I have:
WORKRETAIL | WORKRETAILNAME | WORKRETAILADD | WORKGOV | WORKGOVNAME | WORKGOVADD | WORKTEACH | WORKTEACHNAME | WORKTEACHADD | WORKHEALTH | WORKHEALTHNAME | WORKHEALTHADD | WORKOTHER | WORKOTHERNAME | WORKOTHERADD |
Yes | Salon | 123 ABC St, California | Yes | Agency | 123 ABC St, California | Yes | Elementary | 123 ABC St, California | Yes | Hospital | 123 ABC St, California | No |
Data I need:
WORKNAME1 | WORKADD2 | WORKNAME2 | WORKADD2 | WORKNAME3 | WORKADD3 |
Agency | 123 ABC St, California | Elementary | 123 ABC St, California | Hospital | 123 ABC St, California |
Use variable arrays for searching for yes/no and retrieving corresponding address.
* list the variables in order of importance;
array flags WORKGOV WORKTEACH WORKHEALTH WORKRETAIL WORKOTHER;
array names WORKGOVNAME WORKTEACHNAME WORKHEALTHNAME WORKRETAILNAME WORKOTHERNAME;
array addrs WORKGOVADD WORKTEACHADD WORKHEALTHADD WORKRETAILADD WORKOTHERADD;
array workname(3) $50;
array workadd(3) $100;
index2 = 0;
do index = 1 to dim(flags) while(index2<3);
if flags(index) eq 'No' then continue;
index2+1;
workname(index2) = names(index);
workadd(index2) = addrs(index);
end;
Use variable arrays for searching for yes/no and retrieving corresponding address.
* list the variables in order of importance;
array flags WORKGOV WORKTEACH WORKHEALTH WORKRETAIL WORKOTHER;
array names WORKGOVNAME WORKTEACHNAME WORKHEALTHNAME WORKRETAILNAME WORKOTHERNAME;
array addrs WORKGOVADD WORKTEACHADD WORKHEALTHADD WORKRETAILADD WORKOTHERADD;
array workname(3) $50;
array workadd(3) $100;
index2 = 0;
do index = 1 to dim(flags) while(index2<3);
if flags(index) eq 'No' then continue;
index2+1;
workname(index2) = names(index);
workadd(index2) = addrs(index);
end;
You're a godsend @RichardDeVen . Thank you so much! It worked perfectly.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.