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

Wondering if anyone has a good site to convert R code to SAS code. I was wondering if I can get help converting the following code to SAS. Any guidance is appreciative. require(dplyr) source("bisg_proxy/new_func.R") ia_s <-read.csv('ready_for_r.csv', colClasses = 'character') #add geo tract and geo block ia_2<- ia_s %>% mutate(geo_tract=paste(State, County, Censustrac, sep=''), geo_tract=gsub('[.]','',geo_tract))%>% mutate(geo_block=paste(State, County, Censustrac, BlockGrp, sep=''), geo_block=gsub('[.]','',geo_block)) # if blockgrp == 0, BISG at the tract level #note duplicated primary as secondary for name/dob as no secondary in file # if blockgrp == 0, BISG at the tract level ia_t <- ia_2 %>% filter(BlockGrp %in% c("0",NA)) %>% applyProxy(pri_first_name = 'pri_first_name', pri_last_name = 'pri_last_name', sec_first_name = 'sec_first_name', geoid = 'geo_tract', geoid_key = 'GeoInd', threshold = .75, keepall = T, census_geog = tract_attr_over18) # if blockgrp != 0, BISG at the block group level ia_b <- ia_2 %>% filter(BlockGrp != '0') %>% applyProxy(pri_first_name = 'pri_first_name', pri_last_name = 'pri_last_name', sec_first_name = 'sec_first_name', geoid = 'geo_block', geoid_key = 'geoind', threshold = .75, keepall = T, census_geog = blkgrp_attr_over18) ia_p <- rbind(ia_t, ia_b) %>% arrange(race, gender) #update header names if applicable ia_p_s <- ia_p %>% select(application_id, gender, race, pri_femaleprob:sec_maleprob, Probability_Asian:Probability_White, pri_address, pri_city, pri_state, pri_zip, State, County, Censustrac, BlockGrp, MSA, pri_first_name, pri_last_name) # this exports your file into your folder as a .csv write.csv(ia_p_s, file='proxied.csv', row.names=FALSE

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
I'm familiar with all except applyProxy, do you have a reference for that command, not finding it online either. From the package name it appears to be a fuzzy match for names resulting in probability of gender/ethnicity? I don't think SAS will have a replacement for that.

Either way, most appear to be select or computations, so you can easily use Base SAS to do all of this.

Video.sas.com has most of what you need.

PROC IMPORT will read CSV or Excel files.

CATX() function replaces paste()
WHERE will filter and can be used in almost all procedures.
SAS has had IN for a long time so that's comparable.

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

You need to re-format your message so it is readable.

 

Click on the {i} icon and paste your R-code into that window. Make sure you have one command on one line; not many commands on one line.

--
Paige Miller
Reeza
Super User

There is no automatic conversion as far as I know. Can you post your code reformatted to be legible and then maybe we can offer some options? 

 

I think if you post your code using a code block (6/7th icon) in the Rich Text editor. 

 


@asherer wrote:

Wondering if anyone has a good site to convert R code to SAS code. I was wondering if I can get help converting the following code to SAS. Any guidance is appreciative. require(dplyr) source("bisg_proxy/new_func.R") ia_s <-read.csv('ready_for_r.csv', colClasses = 'character') #add geo tract and geo block ia_2<- ia_s %>% mutate(geo_tract=paste(State, County, Censustrac, sep=''), geo_tract=gsub('[.]','',geo_tract))%>% mutate(geo_block=paste(State, County, Censustrac, BlockGrp, sep=''), geo_block=gsub('[.]','',geo_block)) # if blockgrp == 0, BISG at the tract level #note duplicated primary as secondary for name/dob as no secondary in file # if blockgrp == 0, BISG at the tract level ia_t <- ia_2 %>% filter(BlockGrp %in% c("0",NA)) %>% applyProxy(pri_first_name = 'pri_first_name', pri_last_name = 'pri_last_name', sec_first_name = 'sec_first_name', geoid = 'geo_tract', geoid_key = 'GeoInd', threshold = .75, keepall = T, census_geog = tract_attr_over18) # if blockgrp != 0, BISG at the block group level ia_b <- ia_2 %>% filter(BlockGrp != '0') %>% applyProxy(pri_first_name = 'pri_first_name', pri_last_name = 'pri_last_name', sec_first_name = 'sec_first_name', geoid = 'geo_block', geoid_key = 'geoind', threshold = .75, keepall = T, census_geog = blkgrp_attr_over18) ia_p <- rbind(ia_t, ia_b) %>% arrange(race, gender) #update header names if applicable ia_p_s <- ia_p %>% select(application_id, gender, race, pri_femaleprob:sec_maleprob, Probability_Asian:Probability_White, pri_address, pri_city, pri_state, pri_zip, State, County, Censustrac, BlockGrp, MSA, pri_first_name, pri_last_name) # this exports your file into your folder as a .csv write.csv(ia_p_s, file='proxied.csv', row.names=FALSE


 

asherer
Obsidian | Level 7

Sorry not sure what I did when I pasted it the first time.

 

require(dplyr)

source("bisg_proxy/new_func.R")

ia_s <-read.csv('ready_for_r.csv', colClasses = 'character')


#add geo tract and geo block

ia_2<- ia_s %>%
  mutate(geo_tract=paste(State, County, Censustrac, sep=''),
         geo_tract=gsub('[.]','',geo_tract))%>%
  mutate(geo_block=paste(State, County, Censustrac, BlockGrp, sep=''),
         geo_block=gsub('[.]','',geo_block))

 

# if blockgrp == 0, BISG at the tract level
#note duplicated primary as secondary for name/dob as no secondary in file

# if blockgrp == 0, BISG at the tract level

ia_t <- ia_2 %>%
  filter(BlockGrp %in% c("0",NA)) %>%
  applyProxy(pri_first_name = 'pri_first_name',
             pri_last_name = 'pri_last_name',
             sec_first_name = 'sec_first_name',
             geoid = 'geo_tract',
             geoid_key = 'GeoInd',
             threshold = .75,
             keepall = T,
             census_geog = tract_attr_over18)


# if blockgrp != 0, BISG at the block group level
ia_b <- ia_2 %>%
  filter(BlockGrp != '0') %>%
  applyProxy(pri_first_name = 'pri_first_name',
             pri_last_name = 'pri_last_name',
             sec_first_name = 'sec_first_name',
             geoid = 'geo_block',
             geoid_key = 'geoind',
             threshold = .75,
             keepall = T,
             census_geog = blkgrp_attr_over18)

ia_p <- rbind(ia_t, ia_b) %>%
  arrange(race, gender)

#update header names if applicable
ia_p_s <- ia_p %>%
  select(application_id, gender, race, pri_femaleprob:sec_maleprob,
         Probability_Asian:Probability_White, pri_address, pri_city, pri_state, pri_zip, State, County, Censustrac, BlockGrp, MSA, pri_first_name, pri_last_name)

# this exports your file into your folder as a .csv
write.csv(ia_p_s, file='proxied.csv', row.names=FALSE)

 

 

 

 

PaigeMiller
Diamond | Level 26

The preferred form is as I described earlier, "Click on the {i} icon and paste your R-code into that window."

 

Could you please do that, this makes code much easier to read.

--
Paige Miller
asherer
Obsidian | Level 7
require(dplyr)
#require(openxlsx)
source("bisg_proxy/new_func.R")


ia_s <-read.csv('Overrides for R.csv', colClasses = 'character')


#ia_s <- read.xlsx('Policy Exceptions for R.xlsx')

#add geo tract and geo block

ia_2<- ia_s %>%
  mutate(geo_tract=paste(state, county, censustrac, sep=''),
         geo_tract=gsub('[.]','',geo_tract))%>%
  mutate(geo_block=paste(state, county, censustrac, blockgrp, sep=''),
         geo_block=gsub('[.]','',geo_block))



# if blockgrp == 0, BISG at the tract level
#note duplicated primary as secondary for name/dob as no secondary in file

# if blockgrp == 0, BISG at the tract level

ia_t <- ia_2 %>%
  filter(blockgrp %in% c("0",NA)) %>%
  applyProxy(pri_first_name = 'pri_first_name', 
             pri_last_name = 'pri_last_name',
             sec_first_name = 'sec_first_name',
             geoid = 'geo_tract',
             geoid_key = 'GeoInd',
             threshold = .5,
             keepall = T,
             census_geog = tract_attr_over18)


# if blockgrp != 0, BISG at the block group level
ia_b <- ia_2 %>%
  filter(BlockGrop != '0') %>%
  applyProxy(pri_first_name = 'pri_first_name', 
             pri_last_name = 'pri_last_name',
             sec_first_name = 'sec_first_name',
             geoid = 'geo_block',
             geoid_key = 'geoind',
             threshold = .5,
             keepall = T,
             census_geog = blkgrp_attr_over18)

ia_p <- rbind(ia_t, ia_b) %>%
  arrange(race, gender)

#update header names if applicable 
ia_p_s <- ia_p %>%
  select(application_id, gender, race, pri_femaleprob:sec_maleprob, 
         Probability_Asian:Probability_White, pri_address, pri_city, pri_state, pri_zip, state, county, censustrac, blockgrp, msa, pri_first_name, pri_last_name)

# this exports your file into your folder as a .csv
write.csv(ia_p_s, file='Policy Exceptionsw no DOB proxied.csv', row.names=FALSE)
Reeza
Super User
I'm familiar with all except applyProxy, do you have a reference for that command, not finding it online either. From the package name it appears to be a fuzzy match for names resulting in probability of gender/ethnicity? I don't think SAS will have a replacement for that.

Either way, most appear to be select or computations, so you can easily use Base SAS to do all of this.

Video.sas.com has most of what you need.

PROC IMPORT will read CSV or Excel files.

CATX() function replaces paste()
WHERE will filter and can be used in almost all procedures.
SAS has had IN for a long time so that's comparable.
Reeza
Super User
SAS DataFlux may be able to do some of this. And of course the accuracy depends highly on the data and where you're using it. Due to the racial demographics of where I live, it would be almost useless.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 6276 views
  • 0 likes
  • 3 in conversation