BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DavidPhillips2
Rhodochrosite | Level 12

I'm trying to check if a table has a column.

 

proc sql;
select count(*) into: CompctTableSumOutCount from CombinedpctTableSummaryOut;

quit;

 

ERROR: Table WORK.COMBINEDPCTTABLESUMMARYOUT doesn't have any columns. PROC SQL requires each of its tables to have at least 1
column.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Use PROC SQL to query the DICTIONARY.TABLES table.

 

proc sql;
    select nvar into :nvar from dictionary.tables where libname='WORK' and upcase(memname)='COMBINEDPCTTABLESUMMARYOUT';
quit;

 

--
Paige Miller

View solution in original post

14 REPLIES 14
PaigeMiller
Diamond | Level 26

Use PROC SQL to query the DICTIONARY.TABLES table.

 

proc sql;
    select nvar into :nvar from dictionary.tables where libname='WORK' and upcase(memname)='COMBINEDPCTTABLESUMMARYOUT';
quit;

 

--
Paige Miller
DavidPhillips2
Rhodochrosite | Level 12

note you need to add seperated by ''

 

 

select count(memname) into: CompctTableSumOutCount separated by '' from dictionary.columns
WHERE UPCASE(LIBNAME)="WORK" and memname = 'COMBINEDPCTTABLESUMMARYOUT';

novinosrin
Tourmaline | Level 20

HI @DavidPhillips2 why separated by

 

to park the value of one var into one macro var???

DavidPhillips2
Rhodochrosite | Level 12

You need separated by to remove the extra white space after the value.

Tom
Super User Tom
Super User

@DavidPhillips2 wrote:

You need separated by to remove the extra white space after the value.


That is what the TRIMMED keyword is for.

PaigeMiller
Diamond | Level 26

@DavidPhillips2 wrote:

You need separated by to remove the extra white space after the value.


In general you don't need this, for a specific use you might need to remove extra white space. But when I try this, I don't get white space after the expected zero in the macro variable.

--
Paige Miller
pink_poodle
Barite | Level 11

Here is a nice article about using dictionary tables and views:

 

http://www2.sas.com/proceedings/sugi30/070-30.pdf

r_behata
Barite | Level 11

Try This :

 

%let dsid=%sysfunc(open(sashelp.class));
%let ncols=%sysfunc(attrn(&dsid,nvars));

%put &dsid &ncols;


%macro test;
%if &ncols > 0 %then %do;
proc sql ;
select count(*) into: CompctTableSumOutCount from sashelp.class;
quit;
%end;
%mend;

%test

novinosrin
Tourmaline | Level 20

Why not just proc contents?

r_behata
Barite | Level 11

Just to avoid an Error if there are no columns in the dataset.

novinosrin
Tourmaline | Level 20

Good thinking. 

DavidPhillips2
Rhodochrosite | Level 12

I'm running into a problem where the work table stays around beyond each run of the program.

Ksharp
Super User

/**No variables and No observations**/
data class;
run;

%let dsid=%sysfunc(open(class));
%let ncols=%sysfunc(attrn(&dsid,any));
%let dsid=%sysfunc(close(&dsid));

%put &dsid &ncols;

/******No Observations****************/
data class;
 set sashelp.class;
 stop;
run;

%let dsid=%sysfunc(open(class));
%let ncols=%sysfunc(attrn(&dsid,any));
%let dsid=%sysfunc(close(&dsid));

%put &dsid &ncols;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 14 replies
  • 2093 views
  • 2 likes
  • 8 in conversation