I wanted to retain the label on sas dataset after importing a file from csv file. Can someone please help me with the code. I wrote the following code and the label becoming as blanl on sas file. thanks.
PROC IMPORT DATAFILE="C:\test.csv"
OUT=want
DBMS=csv REPLACE;
GETNAMES=Yes;
GUESSINGROWS=14600;
RUN;
Take the data step from the log and adapt it as needed.
Even better, do not use PROC IMPORT. With a csv file, write the data step yourself in the first place.
If you want to add a member label to your dataset use the LABEL= dataset option when creating the dataset.
out=want(label='Data imported from C:\test.csv')
I think you are asking how to attach labels to the variables created by PROC IMPORT? The proc will not attach labels to variables it creates from reading text files. There is no need to attach labels to every variable because any procedure that prints labels will just print the variable name instead if there is no explicit label attached.
Perhaps you would like to know how to attach the column headers from the CSV as labels on the variables that PROC IMPORT created for those columns? If so then just re-read the header row and use it to generate LABEL statement(s). First you need to know what names PROC IMPORT generated for the variables. One easy way is to use PROC TRANSPOSE with OBS=0 dataset option.
filename csv 'c:\test.csv';
proc import datafile=csv dbms=csv out=want replace ;
run;
proc transpose data=want(obs=0) out=names;
var _all_;
run;
filename labels temp;
data names ;
length _name_ $51 _label_ header $256 ;
set names end=eof;
infile csv dsd obs=1 truncover;
input header @@ ;
_label_=coalescec(header,_name_);
_name_=nliteral(_name_);
file labels ;
if _n_=1 then put 'label' ;
put @3 _name_ '=' _label_ :$quote. ;
if eof then put ';' ;
run;
proc datasets lib=work nolist;
modify want;
%include labels / source2;
run;
quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.