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

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
1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

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;

View solution in original post

2 REPLIES 2
RichardDeVen
Barite | Level 11

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;
cgates
Obsidian | Level 7

You're a godsend @RichardDeVen . Thank you so much! It worked perfectly.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 5722 views
  • 0 likes
  • 2 in conversation