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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.