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.
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;
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;
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).
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.