BookmarkSubscribeRSS Feed
sri1
Obsidian | Level 7

Hi all,

 

I am creating a dataset(out=dsname) for variable attributes (name,type,format,informat) with proc contents.

Can you help me to fix the below

  1. My output dataset shows variable labels instead of variable names .I need the output dataset to show default variable names.
  2. Formats associated with variable doesn't appear in dataset but appears in listing output.
  3. Can I get the varnum in ascending order in dataset instead of default without using proc sort.

Here is my code.

 

proc contents data=dsn out=o_dsn(keep=name type format informat varnum) noprint;

run;

 

Your solutions are highly appreciated.

 

Thanks

11 REPLIES 11
mkeintz
PROC Star

@sri1 

 

Please show us what you have tried, and what the undesired results are.  Help us help you.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Astounding
PROC Star

Agreed, you need to tell us more.  The program you supplied does not create any printed output.  So it's not clear what you are looking at.

 

Just to answer a piece of your question, this is allowed:

 

proc contents data=dsn out=o_dsn(keep=name type format informat) noprint order=varnum;

run;

sri1
Obsidian | Level 7

@Astounding Thanks for your reply.The code I have put doesn't create any printed output but I have test the code with and without noprint option.

order=varnum doesn't sort the varnum in the output dataset (o_dsn) 

mkeintz
PROC Star

by "doesn't create any printed output" I presume you mean the LST output.  But let us see the log, which is where SAS tells you what it thinks is going on.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
sri1
Obsidian | Level 7

@mkeintz  Yes it is listing output..The log doesn't show any warnings

 

Astounding
PROC Star

Then you may have to switch gears.  Instead of PROC CONTENTS, use PROC SQL to extract the information from DICTIONARY.COLUMNS.  SQL supports an order by clause.

Tom
Super User Tom
Super User

@sri1 wrote:

Hi all,

 

I am creating a dataset(out=dsname) for variable attributes (name,type,format,informat) with proc contents.

Can you help me to fix the below

  1. My output dataset shows variable labels instead of variable names .I need the output dataset to show default variable names.
  2. Formats associated with variable doesn't appear in dataset but appears in listing output.
  3. Can I get the varnum in ascending order in dataset instead of default without using proc sort.

Here is my code.

 

proc contents data=dsn out=o_dsn(keep=name type format informat varnum) noprint;

run;

 

Your solutions are highly appreciated.

 

Thanks


You are only keeping some of the information that PROC CONTENTS generates in the OUT= dataset because of the KEEP= dataset option that you are using.  You are keeping the NAME of the variable, but you are not keep the variable's LABEL. Which is the opposite of what you are saying in the text.  The OUT= dataset should be sorted by NAME. If you want it sorted by VARNUM add a PROC SORT step.  If you want to report that PROC CONTENTS produces to also be sorted by order then use the VARNUM option on the PROC statement.

data class ;
  set sashelp.class (keep=name age);
  label name='Student Name';
run;
proc contents data=class out=want(keep=varnum name label type form: inform:) varnum;
run;
proc sort data=want;
  by varnum;
run;
proc print data=want;
run;
SAS 9.4 on WINDOWS

The CONTENTS Procedure

Data Set Name        WORK.CLASS                    Observations          19
Member Type          DATA                          Variables             2
Engine               V9                            Indexes               0
Created              10/28/2019 13:04:45           Observation Length    16
Last Modified        10/28/2019 13:04:45           Deleted Observations  0
Protection                                         Compressed            NO
Data Set Type                                      Sorted                NO
Label
Data Representation  WINDOWS_64
Encoding             wlatin1  Western (Windows)


                                     Engine/Host Dependent Information

Data Set Page Size          65536
Number of Data Set Pages    1
First Data Page             1
Max Obs per Page            4062
Obs in First Data Page      19
Number of Data Set Repairs  0
ExtendObsCounter            YES
Filename                    ....\_TD33560_AMRL20L6F1E4992_\class.sas7bdat
Release Created             9.0401M5
Host Created                X64_10PRO
Owner Name                  ...
File Size                   128KB
File Size (bytes)           131072


SAS 9.4 on WINDOWS

 The CONTENTS Procedure

         Variables in Creation Order

#    Variable    Type    Len    Label

1    Name        Char      8    Student Name
2    Age         Num       8


SAS 9.4 on WINDOWS

Obs   NAME   TYPE   VARNUM      LABEL       FORMAT   FORMATL   FORMATD   INFORMAT   INFORML   INFORMD

 1    Name     2       1     Student Name               0         0                    0         0
 2    Age      1       2                                0         0                    0         0

sri1
Obsidian | Level 7

@Tom I just want to make clear what I mean by 

My output dataset shows variable labels instead of variable names .I need the output dataset to show default variable names.

For example I use the following code

proc contents data=sashelp.zipcode out=o_dsn(keep=name type format informat varnum) ;
run;

 

My output dataset appears as below (labels appear as column headers instead of default names such as  "Variable Name" for "Name"

Capture.PNG

 

From the listing output below they are three formats but in the above dataset,formats doesn't appear.Capture2.PNG

 

Hope the above information helps

 

Thanks

Tom
Super User Tom
Super User

Your question still appear a little muddled.  But I think I can clear up one thing for you.

The FORMAT variable in the output dataset from PROC CONTENTS just holds the name of the format. The part before the width.  The width and the decimal width are stored in two other numeric variables.  Since you did not include those two variables they are not appearing in your dataset.   If you want to build up a full format specification that you could use in a FORMAT statement then you will need to piece them together again.

length formatspec $43;
if formatd then formatspec=cats(format,formatl,'.',formatd);
else if formatl then formatspec=cats(format,formatl,'.');
else if format ne ' ' then formatspec=cats(format,'.');
else formatspec=' ';
Kurt_Bremser
Super User

Use DICTIONARY.COLUMNS:

proc sql;
create table o_dsn as
select name, type, format, informat,varnum
from dictionary.columns
where libname = 'SASHELP' and memname = 'ZIPCODE';
quit;

proc print data=o_dsn noobs;
run;

Result:

name           type    format    informat    varnum

ZIP            num      Z5.                     1  
X              num      11.6                    2  
Y              num      11.6                    3  
ZIP_CLASS      char                             4  
CITY           char                             5  
STATE          num                              6  
STATECODE      char                             7  
STATENAME      char                             8  
COUNTY         num                              9  
COUNTYNM       char                            10  
MSA            num                             11  
AREACODE       num                             12  
AREACODES      char                            13  
TIMEZONE       char                            14  
GMTOFFSET      num                             15  
DST            char                            16  
PONAME         char                            17  
ALIAS_CITY     char                            18  
ALIAS_CITYN    char                            19  
CITY2          char                            20  
STATENAME2     char                            21  

 

Reeza
Super User

As someone else mentioned you want to use the dictionary table instead.

 

data want;
set sashelp.vcolumn;

*keep libname memname name format informat label varnum;

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

run;

Change only the WHERE statement and modify the KEEP statement, currently commented out, to include/exclude the variables of interest. You'll need to verify the names because I'm writing this off memory but it will work easier than PROC CONTENTS. 

 

If the variable does not have a label, by default hte label name becomes the variable name. You could account for that by using the COALESCEC() function in your data step as well.

 

displayLabel = coalescec(label, name);

@sri1 wrote:

Hi all,

 

I am creating a dataset(out=dsname) for variable attributes (name,type,format,informat) with proc contents.

Can you help me to fix the below

  1. My output dataset shows variable labels instead of variable names .I need the output dataset to show default variable names.
  2. Formats associated with variable doesn't appear in dataset but appears in listing output.
  3. Can I get the varnum in ascending order in dataset instead of default without using proc sort.

Here is my code.

 

proc contents data=dsn out=o_dsn(keep=name type format informat varnum) noprint;

run;

 

Your solutions are highly appreciated.

 

Thanks


 

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
  • 11 replies
  • 4508 views
  • 1 like
  • 6 in conversation