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.
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.