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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.