Skip to content

Windows path backward slash

7 messages · Anbu A, Jeff Newmiller, Bill Dunlap +2 more

#
Hi All,
I am a newbie. This is my first program.
I am trying to read SAS dataset from below path. I added escape "\" along
"\" found in path C:\Users\axyz\Desktop\sas\  but still not working.

fsasdat<-function(dsn) {
  pat="C:\\Users\\axyz\\Desktop\\sas\\"
  str1=str_c(pat,dsn,".sas7bdat","\n")
  allmetrx=read_sas(str1)
}
fsasdat("all")

Please help me.

Thanks,
AA.
#
Simplest suggestion is to forget turning this into a function. Alternatively, remove the "allmetrx=" from the last line of your function, as the assignment suppresses automatic printing of the result. However, it may already be working... you could assign the result of the function call outside the function and see what is in that new variable:

ans <- fsasdat("all")
str(ans)
On December 23, 2020 7:08:05 PM PST, Anbu A <rquestion2020 at gmail.com> wrote:

  
    
#
Thanks, Jeff. The reason I am creating the function is I have to read some
more datasets too. I have also modified the code as per your suggestion. I
have to send the dataset name in parameter in subsequent calls.

fsasdat<-function(dsn) {
  pat="C:\\Users\\Anbu\\Desktop\\sas\\"
  str1=str_c(pat,dsn,".sas7bdat","\n")
  read_sas("C:\\Users\\Anbu\\Desktop\\sas\\all.sas7bdat")
}
allmetrx=fsasdat("all")
This code works but if I use  read_sas(str1) it is not working. Any
suggestions, please let me know.

Thanks,
Anbu.

On Thu, Dec 24, 2020 at 9:28 AM Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote:

  
  
#
The "\n" is probably not in the file name.  Does omitting it from the call
to str_c help?

-Bill
On Thu, Dec 24, 2020 at 6:20 AM Anbu A <rquestion2020 at gmail.com> wrote:

            

  
  
#
Hi Bill,
  r"{C:\Users\Anbu\Desktop\sas\}"  - This is the key and code below worked.
fsasdat<-function(dsn) {
  pat=r"{C:\Users\Anbu\Desktop\sas\}"
  str1=str_c(pat,dsn,".sas7bdat")
  read_sas(str1)
#return(str1)
}
allmetrx=fsasdat("all")
str(allmetrx)

Thank you.

Anbu.


On Thu, Dec 24, 2020 at 12:12 PM Bill Dunlap <williamwdunlap at gmail.com>
wrote:

  
  
#
In Windows versions of R/RStudio when refering to filename paths, you  
need to either use two "\\" characters instead of one, OR use the  
reverse slash "/" as used in Linux/Unix. It's an unfortunate conflict  
between R and Windows in that a single \ character by itself is  
treated as an escape character.

It's all Microsoft's fault for using the wrong direction slash in  
MS-DOS and not conforming to Unix style c. 1980.




Quoting Anbu A <rquestion2020 at gmail.com>:
#
What happens if you go to the import tool in RStudio?

It should do your bidding, and, it produces code which
you then can copy and paste.

el
On 2020-12-24 19:31 , Anbu A wrote: