Help using Base SAS procedures

Count number of variables in dataset - for DDE

Reply
Frequent Contributor
Posts: 117

Count number of variables in dataset - for DDE

Hi guys,

I have used the following macro to count the number of rows and use it in my dde:

data _NULL_;

if   0 then set TEST nobs=nr;

index=nr+1;

CALL SYMPUT('row2', PUT(index, 5.));

stop;

run;

usage in DDE: FILENAME dataZ13 DDE "excel|[&template.]&section2.!r2c30:r&row2.c36" notab;

I am wondering how I can do the same for the number of columns? I have a dataset where we add a new variable (column) every month. I was wondering if there was some similar macro that can count the number of columns so I do not have to keep changing the column limits in DDE.

Thanks.

Valued Guide
Posts: 734

Re: Count number of variables in dataset - for DDE

hi ... one idea (use a separator that's not a character in your data ... I used a TAB) ...

question ...  what's that +1 for (index=nr+1) ... it's added in both examples below

data _null_;

set sashelp.class nobs=obs;

call symputx('obs',obs+1);

call symputx('vars',countc(catx('09'x,of _all_),'09'x)+1);

stop;

run;


or dictionary tables (separated by removes leading spaces in macro vars)...


proc sql noprint;

select nobs+1, nvar into Smiley Surprisedbs separated by ' ', :vars separated by ' '

from dictionary.tables

where libname eq 'SASHELP' and memname eq 'CLASS';

quit;

Grand Advisor
Posts: 9,458

Re: Count number of variables in dataset - for DDE

Or compressing code.

%let dsid=%sysfunc(open(sashelp.class));

%let nvar=%sysfunc(attrn(&dsid,nvar));

%let dsid=%sysfunc(close(&dsid));

%put &nvar ;

Ksharp

Ask a Question
Discussion stats
  • 2 replies
  • 5936 views
  • 1 like
  • 3 in conversation