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: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 494 views
  • 4 likes
  • 2 in conversation