DATA Step, Macro, Functions and more

Help listing variables and types

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Help listing variables and types

Hi All

Please help me. I need a program to check the variables in a dataset and if possible list the variable type.

Example: I have 3 datasets. I need to check the variables in each dataset at the end of the month in order to track changes to the dataset structure and raise a flag if the table structure changes.

Test201207 has 2 variables:           amount, date

Test201208 has 3 variables:          amount, date and customer_id

Test201209 has 4 variables:           amount, date, customer_id and count

Thanks


Accepted Solutions
Solution
‎07-25-2012 09:15 AM
Respected Advisor
Posts: 3,777

Re: Help listing variables and types

I think if you make a grid with memnames down the side and variable names across and put type in the cells you can find all the answers you need with one simple report.

data

   test201207(keep=amount date)

   test201208(keep=amount cdate customer_id rename=(cdate=date))

   test201209(keep=amount date customer_id count)

   ;

   attrib amount length=8;

   attrib date length=8;

   attrib cdate length=$10;

   attrib customer_id length=$10;

   attrib count length=8;

   stop;

   call missing(of _all_);

   run;

proc contents data=work._all_ noprint

   out=contents(keep=memname name type where=(memname eq: 'TEST'));

   run;

proc print;

   run;

proc transpose out=report(drop=_name_ _label_);

   by memname;

   var type;

   id name;

   run;

proc print;

   run;

                                        customer_

Obs     MEMNAME      amount    date        id       count

1     TEST201207       1        1         .          .

2     TEST201208       1        2         2          .

3     TEST201209       1        1         2          1

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: Help listing variables and types

is this what you want:

proc contents data=sashelp.class out=class(keep=name type) noprint;
run;
proc print data=class;run;
                                    Obs    NAME      TYPE

                                       1     Age         1
                                       2     Height      1
                                       3     Name        2
                                       4     Sex         2
                                       5     Weight      1

Contributor
Posts: 36

Re: Help listing variables and types


Thanks for your response Linlin.

Yes but I need to see how it differs from the previous datasets - what new variable has been added.

Super Contributor
Posts: 1,636

Re: Help listing variables and types

How about:

data class1 class2;
  set sashelp.class;
data class1;
  set class1;
  room=3;
data class2;
  set class2;
  grade='a';

proc contents data=class1 out=class_1(keep=name type) noprint;
proc contents data=class2 out=class_2(keep=name type) noprint;
run;
proc sort data=class_1;
  by name;
proc sort data=class_2;
  by name;
run;

data check;
  length status $ 18;
  merge class_1(in=a) class_2(in=b);
  by name;
  if a and b then status='both';
   else if a and not b then status='class_1 only';
     else status='class_2 only';

proc print;run;
                             Obs    status          NAME      TYPE

                               1     both            Age         1
                               2     both            Height      1
                               3     both            Name        2
                               4     both            Sex         2
                               5     both            Weight      1
                               6     class_2 only    grade       2
                               7     class_1 only    room        1

PROC Star
Posts: 1,099

Re: Help listing variables and types

Check out the tables in the DICTIONARY library. They make it very easy to get dataset characteristics in a machine readable form. Setting up a scheme to retain the old settings, and compare them with the new ones, should be very easy. PROC COMPARE might do the job nicely.

Tom

Solution
‎07-25-2012 09:15 AM
Respected Advisor
Posts: 3,777

Re: Help listing variables and types

I think if you make a grid with memnames down the side and variable names across and put type in the cells you can find all the answers you need with one simple report.

data

   test201207(keep=amount date)

   test201208(keep=amount cdate customer_id rename=(cdate=date))

   test201209(keep=amount date customer_id count)

   ;

   attrib amount length=8;

   attrib date length=8;

   attrib cdate length=$10;

   attrib customer_id length=$10;

   attrib count length=8;

   stop;

   call missing(of _all_);

   run;

proc contents data=work._all_ noprint

   out=contents(keep=memname name type where=(memname eq: 'TEST'));

   run;

proc print;

   run;

proc transpose out=report(drop=_name_ _label_);

   by memname;

   var type;

   id name;

   run;

proc print;

   run;

                                        customer_

Obs     MEMNAME      amount    date        id       count

1     TEST201207       1        1         .          .

2     TEST201208       1        2         2          .

3     TEST201209       1        1         2          1

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 239 views
  • 0 likes
  • 4 in conversation