Hi all! Nothing better than trying to debug SAS Code on a Saturday afternoon 🙂
I'm just starting to dig into Twitter analytics with PROC Groovy, using a blog post that I found by Falko Schulz (here) and @FriedEgg's posts through the community, but I'm stuck.
Here's the code that is giving me the problem:
proc groovy classpath="C:/bookdata/TwitterAnalysis/groovy-all-2.3.4.jar";
add classpath="C:/bookdata/TwitterAnalysis/opencsv-3.8.jar";
submit "&JSON_TWEET_FILE." "&CSV_TWEET_FILE.";
import groovy.json.*;
import au.com.bytecode.opencsv.CSVWriter;
def input = new File(args[0]).text;
def output = new JsonSlurper().parseText(input);
def csvoutput = new FileWriter(args[1]);
CSVWriter writer = new CSVWriter(csvoutput);
String[] header = new String[15];
header[0] = "id";
header[1] = "text";
header[2] = "truncated";
header[3] = "created_at";
header[4] = "user.id";
header[5] = "user.name";
header[6] = "user.screen_name";
header[7] = "user.location";
header[8] = "user.description";
header[9] = "user.url";
header[10] = "user.followers_count";
header[11] = "user.friends_count";
header[12] = "user.listed_count";
header[13] = "retweet_count";
header[14] = "favorite_count";
writer.writeNext(header);
output.statuses.each {
String[] content = new String[15];
content[0] = it.id.toString();
content[1] = it.text.toString();
content[2] = it.truncated.toString();
content[3] = it.created_at.toString();
content[4] = it.user.id.toString();
content[5] = it.user.name.toString();
content[6] = it.user.screen_name.toString();
content[7] = it.user.location.toString();
content[8] = it.user.description.toString();
content[9] = it.user.url.toString();
content[10] = it.user.followers_count.toString();
content[11] = it.user.friends_count.toString();
content[12] = it.user.listed_count.toString();
content[13] = it.retweet_count.toString();
content[14] = it.favorite_count.toString();
writer.writeNext(content)
}
writer.close();
endsubmit;
quit;
Based on the troubleshooting tips from @FriedEgg and others, I was able to figure out for example i needed to change \ to / in my file locations. The orginal article also had the Classpaths for the two JAR files in the same classpath statement, but I am still getting the same error: Nothing else in the code was changed from the orignal.
ERROR: The SUBMIT command failed. org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script44.groovy: 2: unable to resolve class au.com.bytecode.opencsv.CSVWriter @ line 2, column 7. import au.com.bytecode.opencsv.CSVWriter; ^ 1 error at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:296) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:860) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:521) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:497) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:474) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:292) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:727) at groovy.lang.GroovyShell.parse(GroovyShell.java:739) at groovy.lang.GroovyShell.parse(GroovyShell.java:766) at groovy.lang.GroovyShell.parse(GroovyShell.java:757) 3303 quit;
So from what I gather, it's something to do with the opencsv JAR file, but I've tried the newest version (3.8) the one in the orginal article (2.3) as well as the most recent groovy-all JAR (2.3.4) and again the one refered to in the blog (2.2.1).
The other PROC Groovy in the code seems to run fine, but it is doing something with the JSON file rather than with the CSV.
Any thoughts?
Thanks so much for your time!
Chris
The most likely answer is simply that you are not using the correct name for the class in your import statement. Versions are important and you are using version 3.8 of opencsv, whereas the code you copied was using version 2.3. During the time between these versions the package name was changed making the class name you are looking for in the version 3.8 Jar file "com.opencsv.CSVWriter"
Hi @FriedEgg! Thanks for the reply. I've tried groovy-all-2.2.1 and 2.3.8, and opencsv-2.3 and 3.8 in all 4 combinations and get the same error. I am off travelling tomorrow and not back until Saturday so probably won't have a chance to look at this again until next weekend.
Thanks so much for your help - have a great day!
Chris
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.