Hello,
If I have a dataset which have a large amount variables with name like colXX. Is it a way to found out how many variables with this type of name and assign it to a macro variable COLNUM?
Thanks.
If they are all of the same type (numeric or character does not matter) then a simple data step should do the job.
data _null_;
if 0 then set have;
array __xx col: ;
call symputx('colnum',dim(__xx));
stop;
run;
Otherwise you could try using the SAS dictionary metadata view/table to check.
proc sql noprint;
select count(*) format=32. into :colnum trimmed
from dictionary.columns
where libname='WORK'
and memname='HAVE'
and upcase(name) eqt 'COL'
;
quit;
If they are all of the same type (numeric or character does not matter) then a simple data step should do the job.
data _null_;
if 0 then set have;
array __xx col: ;
call symputx('colnum',dim(__xx));
stop;
run;
Otherwise you could try using the SAS dictionary metadata view/table to check.
proc sql noprint;
select count(*) format=32. into :colnum trimmed
from dictionary.columns
where libname='WORK'
and memname='HAVE'
and upcase(name) eqt 'COL'
;
quit;
Since 0 is treated as FALSE by SAS the SET statement will never actually execute, but it will be checked by the data step compiler to find the list of variables.
Two reasons.
1) If the HAVE dataset has zero observations the data step will stop at the SET statement and never execute the CALL SYMPUTX()
2) If the HAVE dataset is large it might take more resources to actually read even just the first observation from it.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.