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
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;
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;
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.