<?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: Do loops Proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895728#M353904</link>
    <description>&lt;P&gt;Pick a smaller data set, perhaps SASHELP.CLASS which only has 5 variables and 19 observations.&lt;/P&gt;
&lt;P&gt;Show us what the result from that would look like. Not how to do it. Not any code. Just the result BUT we need to know the contents of the data set you start with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe this will give you some hints.&lt;/P&gt;
&lt;PRE&gt;proc freq data=sashelp.cars;
  ods output onewayfreqs=anexample;
  tables make type origin drivetrain;
run;

data nicer;
   set anexample;
   Varname= scan(table,2);
   varvalue = cats(of f_:);
   keep varname varvalue frequency;
run;

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 25 Sep 2023 21:05:01 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-09-25T21:05:01Z</dc:date>
    <item>
      <title>Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895658#M353873</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;I want to create tables using proc sql. I am looking to write a do loop that will make my job easier. I want to write code that will limit my code/job posted below. The data set is from sashelp.cars.&amp;nbsp; Additionally, is there a way to use proc freq output out to create a table with nlevels then use the nlevels table to create the values for the macro?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select count(distinct make)&lt;BR /&gt;into:cm from &amp;amp;c;&lt;BR /&gt;select distinct make&lt;BR /&gt;into: make1- from &amp;amp;c;&lt;BR /&gt;select count(distinct type)&lt;BR /&gt;into: ct from &amp;amp;c;&lt;BR /&gt;select distinct type&lt;BR /&gt;into:type1- from &amp;amp;c;&lt;BR /&gt;select count(distinct origin)&lt;BR /&gt;into: co from &amp;amp;c;&lt;BR /&gt;select distinct origin&lt;BR /&gt;into: origin1- from &amp;amp;c;&lt;BR /&gt;select count(distinct drivetrain)&lt;BR /&gt;into:cdt from &amp;amp;c;&lt;BR /&gt;select distinct drivetrain&lt;BR /&gt;into:dt1-&lt;BR /&gt;from &amp;amp;c;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2023 13:35:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895658#M353873</guid>
      <dc:creator>Reader587</dc:creator>
      <dc:date>2023-09-25T13:35:04Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895663#M353877</link>
      <description>&lt;P&gt;Not clear to me how a loop would work here, but perhaps this is helpful&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dothis(dsn=,variable=,countname=);
    proc sql;
        select count(distinct &amp;amp;variable) into &amp;amp;countname from &amp;amp;dsn;
        select distinct &amp;amp;variable into : &amp;amp;variable.1- from &amp;amp;dsn;
    quit;
%mend;

%dothis(dsn=sashelp.cars,variable=make,countname=cm)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could use PROC FREQ output to get all of the variable names and variable levels, but I don't really see why you are asking about that in relation to the other part of the question.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2023 13:59:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895663#M353877</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-25T13:59:20Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895673#M353884</link>
      <description>&lt;P&gt;I agree, with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;that your objective isn't clear.&amp;nbsp; Why not use a simple program with no macro language?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=sashelp.cars noprint;
   tables make / out=makes;
   tables type / out=types;
   tables origin / out=origins;
   tables drivetrain / out=drivetrains;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What is the reason for complicating any of this with macro variables?&amp;nbsp; Is there some secret "next step" that would benefit from macro variables?&amp;nbsp; (That next step might also be possible without any macro language, but we would need to know what it is.)&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2023 14:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895673#M353884</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-09-25T14:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895674#M353885</link>
      <description>I need the count(distinct &amp;amp;variable) to be a macro.&lt;BR /&gt;For example, when you call the macro, the parameter countname=cm. However, I do not want to have to put in a number.</description>
      <pubDate>Mon, 25 Sep 2023 14:56:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895674#M353885</guid>
      <dc:creator>Reader587</dc:creator>
      <dc:date>2023-09-25T14:56:11Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895676#M353887</link>
      <description>&lt;P&gt;The next step is to create a table for all values of make, origin, and etc...&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2023 15:03:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895676#M353887</guid>
      <dc:creator>Reader587</dc:creator>
      <dc:date>2023-09-25T15:03:05Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895677#M353888</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/401191"&gt;@Reader587&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The next step is to create a table for all values of make, origin, and etc...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What sort of table? You can do that without macro variables just using select distinct with create table.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2023 15:08:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895677#M353888</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-25T15:08:11Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895679#M353889</link>
      <description>&lt;P&gt;But I wouldn't want to do those 35 times.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2023 15:10:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895679#M353889</guid>
      <dc:creator>Reader587</dc:creator>
      <dc:date>2023-09-25T15:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895702#M353891</link>
      <description>&lt;P&gt;Those 35 tables are one for each unique category or combinations of categories?&lt;/P&gt;
&lt;P&gt;This is your method of approaching the problem, but there are other, more optimal ways.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately your method is complicated and difficult to scale well, which is why others are asking for the ultimate goal. Also this question is commonly asked, and 90% of the time, a user doesn't realize there's an easier, more efficient way to do this type of processing in SAS.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://xyproblem.info/" target="_blank"&gt;https://xyproblem.info/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;The XY problem is asking about your attempted&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;solution&lt;/I&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;rather than your actual&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;problem&lt;/I&gt;. This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;User wants to do X.&lt;/LI&gt;
&lt;LI&gt;User doesn't know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y.&lt;/LI&gt;
&lt;LI&gt;User doesn't know how to do Y either.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;User asks for help with Y.&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve.&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn't even a suitable solution for X.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;STRONG&gt;The problem occurs when people get stuck on what they believe is the solution and are unable step back and explain the issue in full.&lt;/STRONG&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Mon, 25 Sep 2023 15:54:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895702#M353891</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-09-25T15:54:22Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895708#M353895</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/401191"&gt;@Reader587&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I need the count(distinct &amp;amp;variable) to be a macro.&lt;BR /&gt;For example, when you call the macro, the parameter countname=cm. However, I do not want to have to put in a number.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So terminology is important to convey clear ideas. The count cannot be a macro. It might be useful to make count a macro variable (macros are not the same as macro variables), but the question remains ... why? People are asking why — many times the original question assumes a macro variable (or a macro) is the way to get to the next step, when in fact often macros and macro variables are not needed. But we need to know what you intend to do next, and this requires explaining the problem at some higher level. We're not asking about coding this in SAS, we're asking about what you are doing, and what are the next steps (table, report, graph, powerpoint, excel file, etc.)&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2023 16:07:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895708#M353895</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-25T16:07:31Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895728#M353904</link>
      <description>&lt;P&gt;Pick a smaller data set, perhaps SASHELP.CLASS which only has 5 variables and 19 observations.&lt;/P&gt;
&lt;P&gt;Show us what the result from that would look like. Not how to do it. Not any code. Just the result BUT we need to know the contents of the data set you start with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe this will give you some hints.&lt;/P&gt;
&lt;PRE&gt;proc freq data=sashelp.cars;
  ods output onewayfreqs=anexample;
  tables make type origin drivetrain;
run;

data nicer;
   set anexample;
   Varname= scan(table,2);
   varvalue = cats(of f_:);
   keep varname varvalue frequency;
run;

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2023 21:05:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895728#M353904</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-25T21:05:01Z</dc:date>
    </item>
    <item>
      <title>Re: Do loops Proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895795#M353922</link>
      <description>&lt;P&gt;No idea what you are trying to do but the posted code is inefficient.&lt;/P&gt;
&lt;P&gt;You appear to running the same query twice just so you can get the COUNT, which SQL will generate automatically for you if you want it ONCE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select count(distinct make) into :cm from &amp;amp;c;
select distinct make into :make1- from &amp;amp;c;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which you can replace with&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select distinct make into :make1- from &amp;amp;c;
%let cm=&amp;amp;sqlobs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to parameterize that then it would help if you used the same string to generate all of the macro variable names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dsname=sashelp.cars;
%let varname=make ;
proc sql noprint;
select distinct &amp;amp;varname into :&amp;amp;varname._1- from &amp;amp;dsname;
%let &amp;amp;varname._n=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;935  options symbolgen;
936  proc sql noprint;
937  select distinct &amp;amp;varname into :&amp;amp;varname._1- from &amp;amp;dsname;
SYMBOLGEN:  Macro variable VARNAME resolves to make
SYMBOLGEN:  Macro variable VARNAME resolves to make
SYMBOLGEN:  Macro variable DSNAME resolves to sashelp.cars
938  %let &amp;amp;varname._n=&amp;amp;sqlobs;
SYMBOLGEN:  Macro variable VARNAME resolves to make
SYMBOLGEN:  Macro variable SQLOBS resolves to 38
939  quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


940  %put &amp;amp;=make_1 &amp;amp;=make_38 ;
SYMBOLGEN:  Macro variable MAKE_1 resolves to Acura
SYMBOLGEN:  Macro variable MAKE_38 resolves to Volvo
MAKE_1=Acura MAKE_38=Volvo
&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Sep 2023 20:54:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loops-Proc-sql/m-p/895795#M353922</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-25T20:54:38Z</dc:date>
    </item>
  </channel>
</rss>

