Skip to content

Instantly share code, notes, and snippets.

@cjdinger
Last active August 16, 2023 10:42
Show Gist options
  • Save cjdinger/2950c1a62acd7f50c33473c311b0d9fe to your computer and use it in GitHub Desktop.
Save cjdinger/2950c1a62acd7f50c33473c311b0d9fe to your computer and use it in GitHub Desktop.
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;
@armandovl
Copy link

Muchas gracias amigo!!!

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