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

Hello,

 

I have a field in the data table that looks like following. I want to extract patient data such as fullUrl, fgiven name, family name, gender, birth date etc. from this string and create variables. Can someone please assist with this? The source data table has thousands of rows so looking for an efficient way to extract these data. Thanks! 

 

"{""fullUrl"": ""urn:uuid:XXXXX-XXXX-XXXX-XXX-XXXXXXXXXX"",
""resource"": {""meta"":
{""profile"": [""WEB-URL""]},
""name"": [{""use"": ""usual"", ""text"": ""FTest LTest"", ""given"": [""FTest""],
""family"": ""LTest""}], ""gender"": ""male"", ""address"": [{""use"": ""home"",
""city"": ""TestSt"", ""line"": [""ST ADDRESS""], ""state"": ""XX"",
""country"": ""US"", ""postalCode"": ""99999""}], ""telecom"": [{""use"":
""work"", ""value"": ""1234567890"", ""system"": ""phone""}], ""birthDate"":
""9999-99-99T99:99:99"", ""identifier"": [{""value"": ""1234"", ""system"":
""urn:oid:1.23.456.7.99999999.2.0000""}], ""resourceType"": ""Patient""}}"

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
5 REPLIES 5
Reeza
Super User
Is it by chance a JSON or XML file? If so, there's libname approaches to parse the data rather than manually parsing it.
monali
Obsidian | Level 7

Thanks. It's a postgres database. The original data feeds are XML files but I am working with Postgres. 

Shmuel
Garnet | Level 18

Any wanted variable is preceded in the input text with appropriate string followed by a colon ":", the value and closed with any combination of the delimiters: ']},";

 

without having the input file type, next tested code can be expanded to read all wanted variables:

data a;
  retain line;
  length text $100 line $1000;
  infile cards truncover;
  input text $100. @;
  line = cats(line,text) ;
  if index(text,'}}') then do; output; input; line=' '; end;
  keep line;
cards;
"{""fullUrl"": ""urn:uuid:XXXXX-XXXX-XXXX-XXX-XXXXXXXXXX"",
""resource"": {""meta"":
{""profile"": [""WEB-URL""]},
""name"": [{""use"": ""usual"", ""text"": ""FTest LTest"", ""given"": [""FTest""],
""family"": ""LTest""}], ""gender"": ""male"", ""address"": [{""use"": ""home"",
""city"": ""TestSt"", ""line"": [""ST ADDRESS""], ""state"": ""XX"",
""country"": ""US"", ""postalCode"": ""99999""}], ""telecom"": [{""use"":
""work"", ""value"": ""1234567890"", ""system"": ""phone""}], ""birthDate"":
""9999-99-99T99:99:99"", ""identifier"": [{""value"": ""1234"", ""system"":
""urn:oid:1.23.456.7.99999999.2.0000""}], ""resourceType"": ""Patient""}}"
; run;

%macro chk(str,var);
    %if &var EQ %then %let var = &str;
    ix = index(new_line,"&str"); 
    if ix>0 then &var = scan(substr(new_line,ix),2,':},');
%mend;
data b;
 set a;
     length name $30 family $30 gender $8 birth_date $25 new_line $1000;
     retain name family gender birth_date;
     array ch {*} name family gender birth_date;
     if _N_=1 then call missing(of ch(*));
     new_line = compress(line,'"[]');
     drop line ix new_line;
     
     %chk(given,name);
     %chk(family);
     %chk(gender);
     %chk(birthDate,birth_date);
run;
 

 

monali
Obsidian | Level 7

Thank you! This worked! 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 5 replies
  • 1244 views
  • 0 likes
  • 3 in conversation