Hi everyone, I would like to send a SAS dataset to R with the formatted values. When I use ExportDataSetToR the dataset is sent with the unformatted values. Any help on how I can retain formatted values? Here is a toy example
data have;
input x $;
datalines;
A
B
;
proc format;
value $x 'A'='One'
'B'='Two';
run;
data have; set have; format x $x.; run;
proc iml;
call ExportDataSetToR("have","want");
submit / R;
want
endsubmit;
quit;
In this example the values 'A' and 'B' show in R instead of 'One' and 'Two'
Thanks!
R doesn't have formats, which is why the formats get stripped out (except for data, time, and datetime). If you want to send in the formatted data, you can use the PUT function to create the formatted values in a separate variable:
proc format;
value $x 'A'='One'
'B'='Two';
run;
data have;
input x $;
format x $x.;
datalines;
A
B
;
data want;
set have(rename=(x=xRaw)); /* rename the raw data */
length x $3; /* =max length of formmatted values */
x = put(xRaw, $x.); /* new var w/ formatted values */
run;
proc iml;
call ExportDataSetToR("want","wantInR");
submit / R;
wantInR
endsubmit;
quit;
R doesn't have formats, which is why the formats get stripped out (except for data, time, and datetime). If you want to send in the formatted data, you can use the PUT function to create the formatted values in a separate variable:
proc format;
value $x 'A'='One'
'B'='Two';
run;
data have;
input x $;
format x $x.;
datalines;
A
B
;
data want;
set have(rename=(x=xRaw)); /* rename the raw data */
length x $3; /* =max length of formmatted values */
x = put(xRaw, $x.); /* new var w/ formatted values */
run;
proc iml;
call ExportDataSetToR("want","wantInR");
submit / R;
wantInR
endsubmit;
quit;
As always, thank you Rick
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.