BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Babloo
Rhodochrosite | Level 12

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ErikLund_Jensen
Rhodochrosite | Level 12

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);

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

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?

Kurt_Bremser
Super User

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().

Reeza
Super User

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.


 

ErikLund_Jensen
Rhodochrosite | Level 12

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);

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2272 views
  • 7 likes
  • 5 in conversation