0
%let pgm=utl-haven-and-sasxport-packages-when-r-variable-names-stored-in-v5-file-labels;

Haven and SASxport packages with r variable names stored in v5 export file labels

github

https://github.com/rogerjdeangelis/utl-haven-and-sasxport-packages-when-r-variable-names-stored-in-v5-file-labels

Thank all of the haven developers for enriching the R language.

I am trying to rename the v5 export the 8 charater variable names to the longer R variable names contained
the label area of the V5 transport file. This way R other languages can use the R names.

I am having two issues withe haven write_xpt that SASxport does not have.
SASxport was removed from CRAN?

     1. write_xpt errs out on R column names with a '.' delimiter (Infant.Mortality)
     2. write_xpt can only output to the current working directory

I am prpbably doing something stupoid but it seems that Greg Warnes SASxport is
easier to use and perhaps more mature.

I think Gregs SASxport works with more non standard variable names and labels.
Also SASxport does not have to output to the current working directory.
It is a little more mature than haven, having been enriched many times over the years?

Consider, built in dataset swiss. This dataset has a variable name with
a period delimiter , Infant.Mortality.
This is also an issue with the iris dataframe variable names.

Extra code needed with write_xpt but not SASxport

   1. I had to rename Infant.Mortality to InfantMortality
   2. I had to set the current working directory

The following text was added because stackoverflow would not let me post without extra text. Tried to simplifiy haven write_xpt to be more like SASxport package see the extra code in write_xpt

For some reason stackoverflow wants additionad detail bebore I can post. I need to be able to use long R variable names in other languages, perl, python, SAS, WPS ....

Tried to simplifiy haven write_xpt to be more like SASxport package see the extra code in write_xpt

For some reason stackoverflow wants additionad detail bebore I can post. I need to be able to use long R variable names in other languages, perl, python, SAS, WPS .... Tried to simplifiy haven write_xpt to be more like SASxport package see the extra code in write_xpt

For some reason stackoverflow wants additionad detail bebore I can post. I need to be able to use long R variable names in other languages, perl, python, SAS, WPS ....

/*
| |__   __ ___   _____ _ __
| `_ \ / _` \ \ / / _ \ `_ \
| | | | (_| |\ V /  __/ | | |
|_| |_|\__,_| \_/ \___|_| |_|

*/
proc datasets lib=sd1 nolist nodetails;delete want; run;quit;
%utlfkil(d:/xpt/want_xpt);

%utl_rbeginx;
parmcards4;
library(haven)
library(labelled)
colfix<-sub("\\.","",colnames(swiss))
colnames(swiss) <- colfix;
want <- set_variable_labels(swiss,.labels = colnames(swiss))
setwd("d:/xpt")
write_xpt(want,"want.xpt",version=5)
;;;;
%utl_rendx;

libname xpt xport "d:/xpt/want.xpt";
proc contents data=xpt._all_;
run;quit;

/**************************************************************************************************************************/
/*                                                                                                                        */
/*   Alphabetic List of Variables and Attributes                                                                          */
/*                                                                                                                        */
/* #    Variable    Type    Len    Label                                                                                  */
/*                                                                                                                        */
/* 2    AGRICULT    Num       8    Agriculture                                                                            */
/* 5    CATHOLIC    Num       8    Catholic                                                                               */
/* 4    EDUCATIO    Num       8    Education                                                                              */
/* 3    EXAMINAT    Num       8    Examination                                                                            */
/* 1    FERTILIT    Num       8    Fertility                                                                              */
/* 6    INFANTMO    Num       8    InfantMortality                                                                        */
/*                                                                                                                        */
/**************************************************************************************************************************/


Because SASxport is not in Cran you need to download
https://cran.r-project.org/src/contrib/Archive/SASxport/
You will need to download and install rtools and devtool.
then
install.packages("#.tar.gz", repos = NULL, type="source")


/*                                   _
 ___  __ _ _____  ___ __   ___  _ __| |_
/ __|/ _` / __\ \/ / `_ \ / _ \| `__| __|
\__ \ (_| \__ \>  <| |_) | (_) | |  | |_
|___/\__,_|___/_/\_\ .__/ \___/|_|   \__|
                   |_|
*/

/*----  SASxport handles labels and non standard column names           ----*/
/*----  Do not have to use the current working directory                ----*/
/*----  Simpler                                                         ----*/

proc datasets lib=sd1 nolist nodetails;delete want; run;quit;
%utlfkil(d:/xpt/want_xpt);

%utl_submit_r64x('
library(SASxport);
library(labelled);
want <- set_variable_labels(swiss,.labels = colnames(swiss));
write.xport(want,file="d:/xpt/want.xpt");
');

libname xpt xport "d:/xpt/want.xpt";
proc contents data=xpt._all_;
run;quit;

/**************************************************************************************************************************/
/*                                                                                                                        */
/*     Alphabetic List of Variables and Attributes                                                                        */
/*                                                                                                                        */
/*  #    Variable    Type    Len    Label                                                                                 */
/*                                                                                                                        */
/*  2    AGRICULT    Num       8    Agriculture                                                                           */
/*  5    CATHOLIC    Num       8    Catholic                                                                              */
/*  4    EDUCATIO    Num       8    Education                                                                             */
/*  3    EXAMINAT    Num       8    Examination                                                                           */
/*  1    FERTILIT    Num       8    Fertility                                                                             */
/*  6    INFANT_M    Num       8    Infant.Mortality                                                                      */
/*                                                                                                                        */
/**************************************************************************************************************************/

/*              _
  ___ _ __   __| |
 / _ \ `_ \ / _` |
|  __/ | | | (_| |
 \___|_| |_|\__,_|

*/

The following is added because stackoverflow would not let me post without extra text. Tried to simplifiy haven write_xpt to be more like SASxport package see the extra code in write_xpt

For some reason stackoverflow wants additionad detail bebore I can post. I need to be able to use long R variable names in other languages, perl, python, SAS, WPS ....

Tried to simplifiy haven write_xpt to be more like SASxport package see the extra code in write_xpt

For some reason stackoverflow wants additionad detail bebore I can post. I need to be able to use long R variable names in other languages, perl, python, SAS, WPS ....

Richard
  • 25,390
  • 3
  • 25
  • 38
  • 1
    StackOverflow was asking you for more details because you formatted the beginning of your question as code instead of text. Please edit your question and remove the spaces at the beginning of lines that are not code but you explaining your problem, and then also remove the duplicated explanations. – joanis Aug 18 '23 at 22:24
  • Why don't you just rename the variables to match SAS naming conventions? You can store the original names as the LABEL if it will help you find them later. – Tom Aug 19 '23 at 05:58
  • 1
    V5 Transport file format is limited to 8 byte variable names. Even the labels can only be 40 bytes long. If you use V8 Transport files then you could have 32 bytes for names and 256 bytes for labels. Plus you should have character variables longer then 200 bytes. – Tom Aug 19 '23 at 16:26
  • Hi Roger, good to see you stack overflow (SO). The indented nature of your pasted question lead the SO parser to presume everything is code. The question parser uses *four leading spaces* as an indicator of code content. If you unindent your narrative it will be text. You can also post unindented code by putting it in a 3-grave bounded block `\`\`\` ... many lines of code ... \`\`\`` – Richard Aug 20 '23 at 00:21
  • Thanks for the constructive replies. Will only indent language code. Some issues with V8 transport; 1. not preferred by FDA. 2. Not supported by all of these languages, PERL, SPSS, STATA, Minitab... – Roger DeAngelis Aug 24 '23 at 18:06

0 Answers0