BookmarkSubscribeRSS Feed
Aidaan_10
Calcite | Level 5

Hi,

             I have an csv excel file with lot of columns. I need to import this file and when I import it not all variables are imported as expected...that's because some of the columns length exceeds more than 32 characters...So suppose this example variable has a name valid_sas_long_variablenumber_one with length of 33 and then there is another similar variable called valid_sas_longvariablenumber1  with length of 28 so in my imported dataset the other longer length variable is getting overwritten and sas is considering only one variable valid_sas_longvariablenumber1. Is there a work around for this one. Can someone please help. I have numerous variables like this.

 

Thanks,

Rasikha

4 REPLIES 4
Reeza
Super User
You cannot have variable names over 32 characters and there is currently no workaround for that. You will need to rename them manually in one fashion or another. The easiest method, IMO is to read the file using PROC IMPORT (assuming you have a CSV). Then get the code from the log and fix the names I need to.

Note that a CSV means comma separated value and Excel opens these files, but they're actually text files. And Excel will interpret types as well, so it doesn't always read the files correctly.
Tom
Super User Tom
Super User

In general I find it easier to write my own data steps to read CSV files.  If you do that you can assign any name you want for the columns.

 

I do not have the problem the way you have described it.  When I use PROC IMPORT to read a file where there a names that match in the first 32 bytes then it makes up a unique name for the variable.

 

For example let's make a test file:

filename csv temp;
data _null_;
  file csv dsd ;
  length name $40 ;
  do i=1 to 5 ;
    name = cats(substr(repeat('this_name_is_too_long_',10),1,35),I);
    put name @;
  end;
  put;
  do i=1 to 5;
    put i @;
  end;
  put;
  stop;
run;

And try to read it.

proc import datafile=csv dbms=csv out=test replace ; run;
proc print data=test; run;
proc contents data=test varnum; run;

Results:

        this_name_
       is_too_long_
Obs     this_name_             VAR2            VAR3            VAR4            VAR5

 1                1               2               3               4               5

                        Variables in Creation Order

#    Variable                            Type    Len    Format     Informat

1    this_name_is_too_long_this_name_    Num       8    BEST12.    BEST32.
2    VAR2                                Num       8    BEST12.    BEST32.
3    VAR3                                Num       8    BEST12.    BEST32.
4    VAR4                                Num       8    BEST12.    BEST32.
5    VAR5                                Num       8    BEST12.    BEST32.

Now if you want you can read the first line of the file use it as LABELs for the variables.

proc transpose data=test(obs=0) out=names ;
  var _all_;
run;

filename code temp;
data names ;
  length _name_ $32 _label_ $256 ;
  set names;
  infile csv dsd truncover obs=1;
  input _label_ @@;
  file code;
  put 'label ' _name_ '=' _label_ :$quote. ';' ;
run;

proc datasets nolist lib=work;
  modify test ;
%include code / source2;
  run;
quit;
proc contents data=test varnum; run;

Result

                                      Variables in Creation Order

#  Variable                          Type  Len  Format   Informat  Label

1  this_name_is_too_long_this_name_  Num     8  BEST12.  BEST32.   this_name_is_too_long_this_name_is_1
2  VAR2                              Num     8  BEST12.  BEST32.   this_name_is_too_long_this_name_is_2
3  VAR3                              Num     8  BEST12.  BEST32.   this_name_is_too_long_this_name_is_3
4  VAR4                              Num     8  BEST12.  BEST32.   this_name_is_too_long_this_name_is_4
5  VAR5                              Num     8  BEST12.  BEST32.   this_name_is_too_long_this_name_is_5

Manvel
Obsidian | Level 7

I have that same issue when i did a transpose and i fixed that with this way.

1)I keep variables which i was needed and after that just done transpose and add By variables which i needed and didn't mentioned VAR that variables just like this.

 

Proc Sort Data = Test; By Var1 var2; Run;

Proc Transpose Data = Test out = Test_;

     By var1, var2;

Run;

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 Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 12655 views
  • 1 like
  • 5 in conversation