BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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;
stataq
Quartz | Level 8
May I ask why we need to use 'if 0 then' instead of directly 'set have'?
Tom
Super User Tom
Super User

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.

SAS Innovate 2025: Register Now

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!

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
  • 3 replies
  • 557 views
  • 4 likes
  • 2 in conversation