<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to get the following combined table using DO loops in SAS? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842214#M333028</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Thank you. But when I run your code, I don't see the number of missing values for the character variables in the final table. I think this could be because your summary table (_summary_t) has 10 rows (only numeric) instead of 15?&lt;/P&gt;</description>
    <pubDate>Wed, 02 Nov 2022 21:26:10 GMT</pubDate>
    <dc:creator>SASuser4321</dc:creator>
    <dc:date>2022-11-02T21:26:10Z</dc:date>
    <item>
      <title>How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842055#M332951</link>
      <description>&lt;P&gt;My goal is to combine these tables into one without having to manually run my macro each time for each column.&lt;/P&gt;&lt;P&gt;The code I currently have with me is the following:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%macro task_Oct(set,col_name);

data _type_;
set &amp;amp;set;
call symputx('col_type', vtype(&amp;amp;col_name));
run;

proc sql; 
create table work.oct27 as
select "&amp;amp;col_name" as variable, 
"&amp;amp;col_type" as type, 
            nmiss(&amp;amp;col_name) as missing_val, 
            count(distinct &amp;amp;col_name) as distinct_val
    from &amp;amp;set;
quit;

%mend task_Oct;

%task_Oct(sashelp.cars,Origin)&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P class=""&gt;The above code gives me the following output:&lt;/P&gt;&lt;PRE class=""&gt;&lt;CODE class=""&gt;|Var   |Type     |missing_val|distinct_val| 
|Origin|Character|0          | 3          | &lt;/CODE&gt;&lt;/PRE&gt;&lt;P class=""&gt;But the sashelp.cars data sheet has 15 columns and so I would like to output a new data sheet which has 15 rows with 4 columns.&lt;/P&gt;&lt;P class=""&gt;I would like to get the following combined table as the output of my code:&lt;/P&gt;&lt;PRE class=""&gt;&lt;CODE class=""&gt;|Var   |Type     |missing_val|distinct_val| 
|Make  |Character|0          | 38         |
|Model |Character|0          | 425        | 
|Type  |Character|0          | 6          | 
|Origin|Character|0          | 3          | 
... ... ... &lt;/CODE&gt;&lt;/PRE&gt;&lt;P class=""&gt;Since I'm using a macro, I could run my code 15 different times by manually entering the names of the columns and then merging the tables into 1; and it wouldn't be a problem. But what if I have a table with 100s of columns? I could use some loop statement but I'm not sure how to go about that in this case. Help would be appreciated. Thank you.&lt;BR /&gt;I wish to solve this task using DO loops.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 11:32:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842055#M332951</guid>
      <dc:creator>SASuser4321</dc:creator>
      <dc:date>2022-11-02T11:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842062#M332956</link>
      <description>&lt;P&gt;Is it a requirement to use Do Loops? And why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or do you simply want a table with col name, type, the number of missing values and the number of distinct values?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 11:54:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842062#M332956</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-02T11:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842065#M332958</link>
      <description>&lt;P&gt;My current code allows me to view the table for one column and I think that if I use DO loops, I can get a table for each column and then if I use PROC Append, I can just stack all the data sets together to get one nice looking table. This way I can also use elements of my current code without them having to go waste and having to rewrite a completely fresh code. Sure, there may be faster ways of doing this (without using DO loops) but I'm currently learning SAS and if there is a way to solve this task with DO loops, I would be interested in that.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 12:09:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842065#M332958</guid>
      <dc:creator>SASuser4321</dc:creator>
      <dc:date>2022-11-02T12:09:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842071#M332962</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/436771"&gt;@SASuser4321&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P class=""&gt;&lt;BR /&gt;I wish to solve this task using DO loops&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In general, it is a mistake to specify the method of solution (in this case a DO loop), rather than to say "This is the output I want". By specifying the method of solution (DO loop), you miss a much simpler solution. Also, DO loops in SAS ought to be the last resort, rather than the first choice.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;PROC FREQ with the NLEVELS option does what you want, without any looping.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods select nlevels;
proc freq nlevels data=sashelp.cars;
tables _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 12:23:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842071#M332962</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-02T12:23:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842074#M332965</link>
      <description>&lt;P&gt;proc freq doesn't tell me that there are 2 missing values in the Cylinders column in the sashelp.cars data set. It counts the 2 "." as 1, while I would like the output table of the code to make it clear that there are indeed 2 missing values.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 12:35:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842074#M332965</guid>
      <dc:creator>SASuser4321</dc:creator>
      <dc:date>2022-11-02T12:35:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842077#M332968</link>
      <description>&lt;P&gt;Okay, got it. You will have to combine data from several sources (PROC FREQ, PROC SUMMARY, PROC CONTENTS), but again no DO loops needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods select none;
proc freq nlevels data= sashelp.cars;
    ods output nlevels=nlevels;
    tables _all_;
run;
ods select all;
proc summary data=sashelp.cars;
    var _numeric_;
    output out=_summary_ nmiss=;
run;
proc transpose data=_summary_(drop=_type_ _freq_) out=_summary_t;
run;
proc sort data=_summary_t;
    by _name_;
run;
proc contents noprint data=sashelp.cars out=_contents_;
run;
proc sort data=nlevels;
    by tablevar;
run;
proc sort data=_contents_;
    by name;
run;
proc format;
    value typef 1='Numeric' 2='Character';
run;
data final;
    merge nlevels(rename=(tablevar=name)) _contents_(keep=name type) _summary_t(rename=(_name_=name col1=n_missing));
    by name;
    format type typef.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Nov 2022 12:42:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842077#M332968</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-02T12:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842080#M332971</link>
      <description>&lt;P&gt;Ok. As pointet out by others, I do not recommend using this method. But since you're learning SAS and this is in a learning context, I have expanded your code a bit to help you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you say Do Loop, I assume you want to use a macro %do loop in your code and do what you've already written for as many variables available in the input data. See the code below and feel free to ask further&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro task_Oct(set);

   proc sql;
      select nvar into :nvar 
      from dictionary.tables
      where libname=upcase(scan("&amp;amp;set.", 1, '.')) 
        and memname=upcase(scan("&amp;amp;set.", 2, '.'));
   quit;

   %do i = 1 %to &amp;amp;nvar.;

      proc sql;
         select  name, type
            into :name, :type
         from dictionary.columns
         where libname=upcase(scan("&amp;amp;set.", 1, '.')) 
           and memname=upcase(scan("&amp;amp;set.", 2, '.'))
         and varnum = &amp;amp;i.;
      quit;

      proc sql; 
      create table table_&amp;amp;i. as
      select distinct "&amp;amp;name" as variable 
           , "&amp;amp;type" as type
           , cmiss(&amp;amp;name) as missing_val
           , count(distinct &amp;amp;name) as distinct_val
          from &amp;amp;set.;
      quit;
   %end;

   data want;
      set table_:;
   run;

%mend task_Oct;

%task_Oct(sashelp.cars)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Nov 2022 13:01:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842080#M332971</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-02T13:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842097#M332979</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&amp;nbsp;In this code, how did you know that we had to write "_numeric_" and not something else after var?&lt;BR /&gt;And what is the purpose of "nmiss="? Thanks.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;proc summary data=sashelp.cars;
    var _numeric_;
    output out=_summary_ nmiss=;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 14:08:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842097#M332979</guid>
      <dc:creator>SASuser4321</dc:creator>
      <dc:date>2022-11-02T14:08:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842144#M332995</link>
      <description>&lt;P&gt;_NUMERIC_ allows PROC SUMMARY to operate on all numeric variables, I don't have to list them by name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NMISS= calculates the number of missing values for each variable in the VAR statement.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 16:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842144#M332995</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-02T16:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842151#M332998</link>
      <description>Thank you. But why is there nothing after the "=" sign after NMISS? NMISS=?</description>
      <pubDate>Wed, 02 Nov 2022 17:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842151#M332998</guid>
      <dc:creator>SASuser4321</dc:creator>
      <dc:date>2022-11-02T17:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842152#M332999</link>
      <description>&lt;P&gt;Nothing after the equal sign sets the new variable name (the name of the new variable that has the count of number of missings) to be the same as the original variable name. On the other hand if you had NMISS=POTATO, then the new variable name is POTATO. It was fine to use NMISS= in this case.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 17:29:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842152#M332999</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-11-02T17:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842214#M333028</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Thank you. But when I run your code, I don't see the number of missing values for the character variables in the final table. I think this could be because your summary table (_summary_t) has 10 rows (only numeric) instead of 15?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 21:26:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842214#M333028</guid>
      <dc:creator>SASuser4321</dc:creator>
      <dc:date>2022-11-02T21:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the following combined table using DO loops in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842258#M333047</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/436771"&gt;@SASuser4321&lt;/a&gt;&amp;nbsp;did you try my code above?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 07:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-following-combined-table-using-DO-loops-in-SAS/m-p/842258#M333047</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-11-03T07:53:40Z</dc:date>
    </item>
  </channel>
</rss>

