DATA Step, Macro, Functions and more

how to set maximum length for variable name

Accepted Solution Solved
Reply
Regular Contributor
Posts: 218
Accepted Solution

how to set maximum length for variable name

Hi All,

Is there a way to set maximum length of variable name in dataset?

 

I have the following variable:

 

Name1_1_sdfgasd      Name_1_2_sdfsklj    Name_1_3_asdfasd  (in this example first 7 character is unique)

 

I want them as: 

 

Name1_1    Name_1_2   Name_1_3

 

 

Thanks.


Accepted Solutions
Solution
‎06-04-2016 10:45 AM
Super User
Posts: 10,044

Re: how to set maximum length for variable name

You can rename them all the time.


data have;
input Name1_1_sdfgasd      Name_1_2_sdfsklj    Name_1_3_asdfasd   ;
cards;  
100    293       284
100    291       284
;
run;
proc transpose data=have(obs=0) out=temp;
 var _all_;
run;
data temp;
 set temp;
 length name $ 32;
 name=substr(_name_,1,findc(_name_,'_','b')-1);
run;
proc sql noprint;
 select catx('=',_name_,name) into : list separated by ' '
  from temp;
quit;
proc datasets library=work nodetails nolist;
 modify have;
 rename &list ;
quit;
 

View solution in original post


All Replies
Super User
Posts: 11,343

Re: how to set maximum length for variable name

SAS datasets have a design limit of 32 characters maximum.

If you have imported data that yielded names that you do not want there are a number of ways to rename the variables. Manual method would be to open the column view of the Dataset in the SAS Explorer, click on the name and rename.

Other methods involve using Proc Datasets or a rename statement in a data step.

 

Data have;

   set have;

   rename Name1_1_sdfgasd =Name1_1  Name_1_2_sdfsklj =  Name_1_2 <etc>;

run;

Regular Contributor
Posts: 218

Re: how to set maximum length for variable name

Hi ballardw,
Is there a way to do in a more efficient (dynamic) way rather than renaming? Sometimes I will not know the name and number of the variable.

Thanks.
Super User
Posts: 11,343

Re: how to set maximum length for variable name

I am not sure what "dynamic" might mean in this sense. Can you provide a rule that will work every time and be unambiguous? Your example looks like "truncate at the last underscore following a digit". But does that work if you get a variable of Name_1_4_pd3_abc

and you want to truncate at the 4?

 

I would look into where these names are coming from in the first place. I suspect they are from Proc Import reading a file with long headers. If the source files were of a common layout but are just getting headers changed such as Var_1_March in one file and Var_1_April in the next but basically should just be Var_1 then I would look into my import procedure to read with the correct names and other properties.

 

The alternate approach would have to be along the lines of 1) get the current names (Proc Contents or from sashelp.vcolumns) in a data set, 2) use appropriate rules to create a new variable with the modified name and 3) use the results to rename the variables (proc datasets or a rename statement).

Solution
‎06-04-2016 10:45 AM
Super User
Posts: 10,044

Re: how to set maximum length for variable name

You can rename them all the time.


data have;
input Name1_1_sdfgasd      Name_1_2_sdfsklj    Name_1_3_asdfasd   ;
cards;  
100    293       284
100    291       284
;
run;
proc transpose data=have(obs=0) out=temp;
 var _all_;
run;
data temp;
 set temp;
 length name $ 32;
 name=substr(_name_,1,findc(_name_,'_','b')-1);
run;
proc sql noprint;
 select catx('=',_name_,name) into : list separated by ' '
  from temp;
quit;
proc datasets library=work nodetails nolist;
 modify have;
 rename &list ;
quit;
 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 1145 views
  • 0 likes
  • 3 in conversation