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

I need some code that will loop through all variables in a dataset, and check each variable for missing values.

If any missing are found then fill with # characters (or value 9) based on the length of that particular variable.

Here is my poor attempt:

data want;
dsid=open("have", "i"); 
n_vars=attrn(dsid, "nvars");
do i=1 to n_vars;
Name=varname(dsid, i);
Type=vartype(dsid, i);

set have;

if missing(Name) then do;
	if Type = 'C' then 
		Name = repeat('#', length(Name));
	else	
		Name = repeat(9, length(Name));
	end; 

end;
rc=close(dsid);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

In case you just want to do this for reporting purposes then consider to not change the data but to use formats and/or options missing='#'; instead.

View solution in original post

4 REPLIES 4
qoit
Pyrite | Level 9

At its very minimum, below should do the job. Also REPEAT function returns a character value so I'd suggest keeping missing numeric values as "99" or "999", below:

 

data have;
set sashelp.class;
if name = 'James' then call missing(sex,weight);
run;

%macro test;


%local i;

proc sql;
	select name,type,length
		into: name1 - :name&sysmaxlong, 
		:type1 - :type&sysmaxlong,
		:len1 - :len&sysmaxlong
	from dictionary.columns
		where libname = 'WORK'
			and memname = 'HAVE';
	%let cnt = &sqlobs;
quit;

data want;
	set have;

	%do i = 1 %to &cnt.;
		%if &&type&i. = char %then
			%do;
				if missing(&&name&i.) then
					&&name&i. = repeat('#', &&len&i.);
			%end;
		%else
			%do;
				if missing(&&name&i.) then
					&&name&i. = 99;

				/*&&name&i. = input(strip(repeat(9, &&len&i.)),8.);*/
			%end;
	%end;
run;

%mend;

%test
simmwa
Fluorite | Level 6

Thanks @qoit it works perfectly !

dscastelino
Calcite | Level 5

Is there a way to include a macro for multiple HAVE datasets. 

Patrick
Opal | Level 21

In case you just want to do this for reporting purposes then consider to not change the data but to use formats and/or options missing='#'; instead.

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
  • 1696 views
  • 5 likes
  • 4 in conversation