Hi Guys !
Please help me to solve below issue:
I`ve got data string like this : {"labelX":"valueX","labelY":"valueY","labelZ":"valueZ"}
I need to split data by comma, where "label" should be the name of column, and rows should be filled with "value"
thanks in advance
regards
LG.
Well, to be honest that looks a bit Json like to me. Is it only one string, if so then something like:
data have; txt='{"labelX":"valueX","labelY":"valueY","labelZ":"valueZ"}'; call execute('data want;'); do i=1 to countw(txt,","); call execute(cat(" length ",compress(scan(scan(txt,i,","),1,":"),'"{}')," $100; ",compress(scan(scan(txt,i,","),1,":"),'"{}'),'="',compress(scan(scan(txt,i,","),2,":"),'"{}'),'";')); end; call execute('run;'); run;
Is probably the quickest way to go. Though I suspect we are only seeing the tiny bit of the picture here. Why do you have that type of string, where does it come from? More information would provide a better solution.
There are a couple of possibilities. Try:
Data A;
Txt=%Unquote('{"labelX":"valueX","labelY":"valueY","labelZ":"valueZ"');
Output;
Run;
Data B (Keep=Name Value);
Set A;
Txt=Compress(Txt,'"[]'); * please replace with squiggly brackets - I can't get them into the editor here;
Txt=Translate(Txt,',',':');
Do i=1 To Count(Txt,',') By 2;
Name=Scan(Txt,i,',');
Value=Scan(Txt,i+1,',');
Output;
End;
Run;
Proc Transpose =B Out=B_T (Drop=_NAME_);
ID Name;
Var Value;
Run;
Well, to be honest that looks a bit Json like to me. Is it only one string, if so then something like:
data have; txt='{"labelX":"valueX","labelY":"valueY","labelZ":"valueZ"}'; call execute('data want;'); do i=1 to countw(txt,","); call execute(cat(" length ",compress(scan(scan(txt,i,","),1,":"),'"{}')," $100; ",compress(scan(scan(txt,i,","),1,":"),'"{}'),'="',compress(scan(scan(txt,i,","),2,":"),'"{}'),'";')); end; call execute('run;'); run;
Is probably the quickest way to go. Though I suspect we are only seeing the tiny bit of the picture here. Why do you have that type of string, where does it come from? More information would provide a better solution.
hello,
data have;
x='{"labelX":"valueX","labelY":"valueY","labelZ":"valueZ"}';
x=prxchange('s/{|}|"//i',-1,x);
x=prxchange('s/:/="/i',-1,x);
x=prxchange('s/,/";/i',-1,x);
call symput ('syn',strip(x)||'";');
run;
data want;
&syn;
run;
Post a sample FILE ,and let us test it.
It is JSON file . and PROC DS2 has JSON package, maybe you should look at it.
Guys,
I`ve got it ! 🙂
thank you All for quick help !!! ( SCAN and easy loop was the solution )
wish you best 🙂
regards
LG
Please mark it as solved 😉
not my message...but the solution you have chosen...
no worries mate 🙂
reg.
LG
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: