Appericiate if someone of you guide me with the program to change the length of the variable for multiple datasets from one library?
Assume the variable name is ID (numeric) and it has different lengths and this variable is located in multiple datasets and those datasets are in one library. Now I want to change the length of ID variable to 8 in all the datasets from all those libraries in one shot.
Hi Babloo
I Think the following macro does the trick. It changes datasets overwriting the original datasets. Write me back if you have any questions.
%macro changelength(folder);
* Assign folder as TMP-library;
libname tmp "&folder";
* Get content of TMP-library;
proc contents data=tmp._all_ out=w noprint;
run;
* Get member names - only members where variable ID exists as numeric with length not 8;
proc sql noprint;
select distinct memname into :memlist separated by ' ' from w
where lowcase(name) = 'id' and type = 1 and length ne 8;
quit;
* Copy to same member, Length statement PRECEDES Set statement to change length;
%do i = 1 %to &sqlobs;
%let mem = %scan(&memlist,&i,%str( ));
data tmp.&mem; length id 8; set tmp.&mem;
run;
%end;
libname tmp clear;
%mend;
%changelength(c:\tmp);
%changelength(c:\etc);
You would have to recreate every relevant data set. Once a variable length is set, it can not be changed.
How do you know which data sets you want to perform this action for?
ID variables should not be kept as numbers, but as character.
Anyway, you find the datasets by searching for the variable name in sashelp.vcolumn, and create the code from that with call execute().
1. Figure out how to do it for one data set
2. Find the list of all data sets you need to do this to (been answered in previous questions)
3. Use CALL EXECUTE to run it for every data set needed.
Example of turning this into a macro is here:
https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
If you need further help post your code.
@Babloo wrote:
Appericiate if someone of you guide me with the program to change the length of the variable for multiple datasets from one library?
Assume the variable name is ID (numeric) and it has different lengths and this variable is located in multiple datasets and those datasets are in one library. Now I want to change the length of ID variable to 8 in all the datasets from all those libraries in one shot.
Hi Babloo
I Think the following macro does the trick. It changes datasets overwriting the original datasets. Write me back if you have any questions.
%macro changelength(folder);
* Assign folder as TMP-library;
libname tmp "&folder";
* Get content of TMP-library;
proc contents data=tmp._all_ out=w noprint;
run;
* Get member names - only members where variable ID exists as numeric with length not 8;
proc sql noprint;
select distinct memname into :memlist separated by ' ' from w
where lowcase(name) = 'id' and type = 1 and length ne 8;
quit;
* Copy to same member, Length statement PRECEDES Set statement to change length;
%do i = 1 %to &sqlobs;
%let mem = %scan(&memlist,&i,%str( ));
data tmp.&mem; length id 8; set tmp.&mem;
run;
%end;
libname tmp clear;
%mend;
%changelength(c:\tmp);
%changelength(c:\etc);
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.