BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I'm looking to transform some data where we have a group of question/answer sections for different options. The basic format is as such:


123

FavoriteColor
blue


FavoriteFood
pizza



There can be a large list of questions/answers but what I really want to do is setup a datasaet with id, FavoriteColor, and FavoriteFood variables. Can I setup the path in an XML map which basically says something such as:

person/qa[question='FavoriteFood']/answer
person/qa[question='FavoriteColor']/answer

Data should look like:

ID FavoriteFood FavoriteColor
123 pizza blue

When I attempt something similar to this in the SAS XML mapper I get no value. Is this even possible to do within a .map file?

I could possibly transpose this via PROC TRANSPOSE, but I am really only interested in mapping a few key values.

Thoughts or suggestions?
5 REPLIES 5
SUN59338
Obsidian | Level 7
try to move id line inside each qa. it works to define a library refer to the file under SAS 9.2 . then you may use proc transpose or other approach to get what you need.




123
FavoriteColor
blue


123
FavoriteFood
pizza



libname test xml "yourxmlfile.xml";
Paul_Kent_SAS
SAS Employee
sorry. you cant use fancy xpath like you want to; You dont have to re-arrange the XML however.

i used the map/transpose technique you described.

here is the xml i used for your data (i added a mystuff root node):



123

FavoriteColor
blue


FavoriteFood
pizza






here is my map:











/mystuff/person/qa


/mystuff/person/id
numeric
integer



/mystuff/person/qa/question
character
string
13



/mystuff/person/qa/answer
character
string
5








and here is my sas job:
filename bryanmat 'C:\Paul\XML\bryanmat.xml';
filename SXLEMAP 'C:\Paul\XML\brianmat.map';
libname bryanmat xml92 xmlmap=SXLEMAP access=READONLY;

data work.person;
set bryanmat.person;
run;

proc print;
title 'before transpose';
run;

proc transpose data=person out=person;
by id;
id question;
var answer;
where question in ('FavoriteColor', 'FavoriteFood');
run;

proc print;
title 'after transpose';
run;
deleted_user
Not applicable
Thanks Paul. I was working on going the PROC TRANSPOSE route but I didn't think of using it with a where clause. I think that helped to solve the missing piece of the puzzle.
Paul_Kent_SAS
SAS Employee
You bet; if the file are really huge, it makes sense to throw the unwanted ones away right when you extract them from the XML...

data work.person;
set bryanmat.person;
where question in ('FavoriteColor', 'FavoriteFood');
run;
Paul_Kent_SAS
SAS Employee
Oops. Sorry about the bryan/brian mix up. Daughters beau is a bryan and my tired fingers musta been on automatic 😞

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1306 views
  • 0 likes
  • 3 in conversation