SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
ctisseuil
Fluorite | Level 6

Hi, 

I do not manage to get column names from my dataset using proc sql.

What's wrong in the following example code ?

 

data work.birthday;
  input @01 employee_id   6.
        @08 last_name     $10.
        @19 birthday      date7.;
  format employee_id   6.
         last_name     $10.
         birthday      date7.;
  datalines;
  1247 Garcia     04APR54
  1078 Gibson     23APR36
  1005 Knapp      06OCT38
  1024 Mueller    17JUN53
;
proc print data=work.birthday;
  title2 'SAS Data Set work.BIRTHDAY';
run;

proc sql;
select * into :charlist separated by ' '
from dictionary.columns
where libname="work" and memname="birthday" 
;
quit;

 

 

 

3 REPLIES 3
FreelanceReinh
Jade | Level 19

Hi @ctisseuil,

  1. Write "select name ..." instead of "select * ..."
  2. Specify libname and memname values in UPPER CASE in the WHERE clause.
Patrick
Opal | Level 21

And adding to @FreelanceReinh : The values for libname and memname are always uppercase

where libname="WORK" and memname="BIRTHDAY"

 

Tom
Super User Tom
Super User

Do you want the full list of problems? Or just the problems with the part that is trying to gather variable names?

Let's start with the last step:

 

proc sql;
select * into :charlist separated by ' '
from dictionary.columns
where libname="work" and memname="birthday" 
;
quit;

Issues:

  • Add the NOPRINT option to the PROC SQL so the names are not also printed to the output window.
  • Specify the actual value you want to put into the macro variable.  This would only select the variable NAME if that happens to be the first variable that would get selected by the * shortcut for all varaibles.
  • Values of LIBNAME and MEMNAME are always uppercase in the DICTIONARY.COLUMNS.
  • Do you care what order the names appear in the list?
  • You also need to protect in case VALIDVARNAME option is set to ANY.  That means some names might have spaces in them.  You can use the NLITERAL() function for example  Or use some other character as the separator in the list for values.
proc sql noprint;
select nliteral(name)
  into :charlist separated by ' '
  from dictionary.columns
  where libname="WORK" and memname="BIRTHDAY"
  order by varnum 
;
quit;

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 1375 views
  • 0 likes
  • 4 in conversation