Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example to read CSV file from the web, import to SAS with nonstandard var names, then rename/relabel to conform with standard SAS variable name rules .
/* Example to read CSV file from the web */
/* Import to SAS with nonstandard var names */
/* Then rename/relabel to conform with standard */
/* SAS variable name rules */
/* Fetch the file from the web site */
filename probly temp;
proc http
url="https://raw.githubusercontent.com/zonination/perceptions/master/probly.csv"
method="GET"
out=probly;
run;
/* Tell SAS to allow "nonstandard" names */
options validvarname=any;
/* import to a SAS data set */
proc import
file=probly
out=work.probly replace
dbms=csv;
run;
/* Generate new names to comply with SAS rules. */
/* Assumes names contain spaces, and can fix with COMPRESS */
/* Other deviations (like names that start with a number) */
/* would need different adjustments */
/* NVALID() function can check that a name is a valid V7 name */
proc sql noprint;
/* retain original names as labels */
select cat("'",trim(name),"'n","=","'",trim(name),"'")
into :labelStmt separated by ' '
from sashelp.vcolumn where memname="PROBLY" and libname="WORK";
select cat("'",trim(name),"'n","=",compress(name,,'kn'))
into :renameStmt separated by ' '
from sashelp.vcolumn where memname="PROBLY" and libname="WORK"
/* exclude those varnames that are already valid */
AND not NVALID(trim(name),'V7');
quit;
proc datasets lib=work nolist ;
modify probly / memtype=data;
label &labelStmt.;
rename &renameStmt.;
/* optional: report on the var names/labels */
contents data=probly nodetails;
quit;
/* reset back to the old rules */
options validvarname=v7;
proc print data=work.probly(obs=10) label ;
run;
@zhanglianbo35

This comment has been minimized.

Copy link

@zhanglianbo35 zhanglianbo35 commented Sep 13, 2018

how to use filename url to read the data on web(https) ? it always shows "connect refused "

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment