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

Hello, I'm trying to figure out how to get the "name" from variable2 that corresponds to the "id" in variable1.

In addition, if variable1 isn't found in variable2 than the output would just be the number in variable1.

Example:

 

Variable1     Variable2                                                                                                                                        Want

1                    <OPTIONS><OPTION id="1" name="Yes"/><OPTION id="2" name="No"/></OPTIONS>          Yes

2                   <OPTIONS><OPTION id="1" name="Yes"/><OPTION id="2" name="No"/></OPTIONS>            No

33                  <OPTIONS/>                                                                                                                                  33

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input var1 $ var2 & $80.;
cards;
1                    <OPTIONS><OPTION id="1" name="Yes"/><OPTION id="2" name="No"/></OPTIONS>       
2                   <OPTIONS><OPTION id="1" name="Yes"/><OPTION id="2" name="No"/></OPTIONS>          
33                  <OPTIONS/>      
;

data want;
 set have;
p=find(var2,cats('id="',var1,'"')); 
if p then want=scan(substr(var2,p),4,'"');
 else want=var1;
run;

View solution in original post

1 REPLY 1
Ksharp
Super User
data have;
input var1 $ var2 & $80.;
cards;
1                    <OPTIONS><OPTION id="1" name="Yes"/><OPTION id="2" name="No"/></OPTIONS>       
2                   <OPTIONS><OPTION id="1" name="Yes"/><OPTION id="2" name="No"/></OPTIONS>          
33                  <OPTIONS/>      
;

data want;
 set have;
p=find(var2,cats('id="',var1,'"')); 
if p then want=scan(substr(var2,p),4,'"');
 else want=var1;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 1 reply
  • 554 views
  • 0 likes
  • 2 in conversation