<?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 automate to find count of variables and then automatically concatinate in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547953#M151900</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250721"&gt;@Mastanvali&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;How to find out total number of variables in a dataset?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You have two answers in this thread already that have provided answers using the metadata views.&lt;/P&gt;
&lt;P&gt;You could also just use PROC CONTENTS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have noprint out=contents; run;
proc sql noprint;
select name into :varlist separated by ' '
from contents
where upcase(name) like 'COL%'
;
%let nvars=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 02 Apr 2019 16:29:41 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-04-02T16:29:41Z</dc:date>
    <item>
      <title>How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547854#M151863</link>
      <description>&lt;P&gt;Hi Team,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have columns in data1 id,col1,col2,col3.&lt;/P&gt;&lt;P&gt;Now I need like&lt;/P&gt;&lt;P&gt;select distinct id, Strip(col1)||strip(col2)||strip(col3) from data1&lt;/P&gt;&lt;P&gt;But now what I need is in future I have columns like col1,col2,col3....col10&lt;/P&gt;&lt;P&gt;at this position i need to automate in select statement at the point of strip function&amp;nbsp;&lt;/P&gt;&lt;P&gt;select distinct id,strip(col1)||strip(col2)||....||strip(col10)&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 11:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547854#M151863</guid>
      <dc:creator>Mastanvali</dc:creator>
      <dc:date>2019-04-02T11:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547858#M151864</link>
      <description>&lt;P&gt;Extracte the names from dictionary.columns, wrap them into strip(), and store them in a macro variable for later use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select "strip(" || trim(name) || ")" into :names separated by '||'
from dictionary.columns
where libname = 'WORK' and memname = 'HAVE' and index(upcase(name),'COL') = 1;
create table want as
select distinct id, &amp;amp;names.
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;Edit: changed the literal comma separator to the double pipe characters. Note that both codes should result in the same number of rows.&lt;/FONT&gt;&lt;/EM&gt; &lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 11:30:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547858#M151864</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-04-02T11:30:07Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547859#M151865</link>
      <description>any body help me out of the issue.&lt;BR /&gt;It's very priority query</description>
      <pubDate>Tue, 02 Apr 2019 11:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547859#M151865</guid>
      <dc:creator>Mastanvali</dc:creator>
      <dc:date>2019-04-02T11:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547878#M151870</link>
      <description>&lt;P&gt;Please try, the new variable generated will have the expected output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=sashelp.vcolumn out=trans (drop=_name_ _label_);
where libname='WORK';
by memname;
id varnum;
var name;
run;

data want;
length variables $2000.;
set trans;
variables=catx(',',of _:);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Apr 2019 12:49:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547878#M151870</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-04-02T12:49:28Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547923#M151885</link>
      <description>&lt;P&gt;Isn't this a bit silly?&amp;nbsp; Why would you want to treat these two combinations as being the same?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;col1='ab'&amp;nbsp; &amp;nbsp;col2='cd'&amp;nbsp; col3='e'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;col1='a'&amp;nbsp; col2='bc'&amp;nbsp; col3='de'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just get rid of the STRIP function entirely, and try to get the equivalent of:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;distinct (id col1 col2 col3)&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 15:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547923#M151885</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-04-02T15:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547929#M151887</link>
      <description>did you get my concern?&lt;BR /&gt;&lt;BR /&gt;My concern is automatically concatenate similar variables.&lt;BR /&gt;</description>
      <pubDate>Tue, 02 Apr 2019 15:31:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547929#M151887</guid>
      <dc:creator>Mastanvali</dc:creator>
      <dc:date>2019-04-02T15:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547935#M151893</link>
      <description>&lt;P&gt;Then you are barking up the wrong tree using SQL.&amp;nbsp; A DATA step can handle variable lists, while SQL cannot:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data joined / view=joined;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;keyvar = cats(id, of col: );&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=joined;&lt;/P&gt;
&lt;P&gt;tables keyvar / noprint out=want;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But my comment is still true.&amp;nbsp; Removing blanks means you could be counting two combinations as being the same when they are different.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 19:42:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547935#M151893</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-04-02T19:42:06Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547936#M151894</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250721"&gt;@Mastanvali&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;did you get my concern?&lt;BR /&gt;&lt;BR /&gt;My concern is automatically concatenate similar variables.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;1) Input data set example (as data step code)&lt;/P&gt;
&lt;P&gt;2) Output data set example (as data step code)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) tell us how to determine variables are "similar".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Showing (incomplete and data less)&amp;nbsp;code that does not work does not describe a problem very well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And almost any problem that involves an unknown number of variables tends to indicate poorly structured data to start with.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 15:50:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547936#M151894</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-04-02T15:50:12Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547939#M151896</link>
      <description>I have variables id col1col2 col3 in datasetname(data1)&lt;BR /&gt;now im writing sql query&lt;BR /&gt;Create table t as&lt;BR /&gt;Select distinct id,strip(col1)||strip(col2)||strip(col3)&lt;BR /&gt;&lt;BR /&gt;Now at present in my data1 dataset I have variables I'd col1 col2&lt;BR /&gt;col3....col8&lt;BR /&gt;Now i need all query like above query only but while new data coming at&lt;BR /&gt;that time col1 col2 like similar variables are getting more or less so we&lt;BR /&gt;need to insert that column names manually right.&lt;BR /&gt;For that I need to write automatically it will take how many similar&lt;BR /&gt;variables we are getting find out that and automatically concatenate&lt;BR /&gt;&lt;BR /&gt;this my concern&lt;BR /&gt;</description>
      <pubDate>Tue, 02 Apr 2019 15:58:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547939#M151896</guid>
      <dc:creator>Mastanvali</dc:creator>
      <dc:date>2019-04-02T15:58:20Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547946#M151898</link>
      <description>&lt;P&gt;One way to think of it is to consider the code you want to generate as text. If you can generate the text then you could put that text into a macro variable and use it at the place you need it in your code.&lt;/P&gt;
&lt;P&gt;So for the simple case you have presented where you have variables that a literally named COL1, COL2, ... COLn you could do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  length code $2000;
  do i=1 to 6 ;
    code=catx('||',code,cats('strip(col',i,')'));
  end;
  call symputx('code',code);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then in your SQL code just reference the macro variable CODE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;... id,&amp;amp;code ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I suspect that your real problem is more complex. The names of the variables are probably not literally COL1, COL2, ...&lt;/P&gt;
&lt;P&gt;Also the code you are generating is most likely not doing what you actually want.&amp;nbsp; If you can explain your actual problem (using actual examples) then someone could provide a solution.&amp;nbsp; For example it might be that your actual problem is that you need to get the distinct values of ALL of the data in the input dataset. In that case you don't need to know the names of ANY of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Use PROC SORT ;
proc sort data=have out=want nodupkey;
  by _all_;
run;
* Use PROC SQL ;
proc sql;
create table want as select distinct * from have ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you did only want the distinct values of some of the variables then perhaps they use a common prefix on their names?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have(keep=id col:) out=want nodupkey;
by _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 16:13:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547946#M151898</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-02T16:13:43Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547950#M151899</link>
      <description>In the above code you gave like&lt;BR /&gt;&lt;BR /&gt;do i=1 to 6 ;&lt;BR /&gt;code=catx('||',code,cats('strip(col',i,')'));&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;you enter manually but i need like&lt;BR /&gt;&lt;BR /&gt;do i=1 to &amp;amp;cnt ;&lt;BR /&gt;code=catx('||',code,cats('strip(col',i,')'));&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;How to find out total number of variables in a dataset?&lt;BR /&gt;</description>
      <pubDate>Tue, 02 Apr 2019 16:20:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547950#M151899</guid>
      <dc:creator>Mastanvali</dc:creator>
      <dc:date>2019-04-02T16:20:20Z</dc:date>
    </item>
    <item>
      <title>Re: How To automate to find count of variables and then automatically concatinate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547953#M151900</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/250721"&gt;@Mastanvali&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;How to find out total number of variables in a dataset?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You have two answers in this thread already that have provided answers using the metadata views.&lt;/P&gt;
&lt;P&gt;You could also just use PROC CONTENTS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have noprint out=contents; run;
proc sql noprint;
select name into :varlist separated by ' '
from contents
where upcase(name) like 'COL%'
;
%let nvars=&amp;amp;sqlobs;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Apr 2019 16:29:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-To-automate-to-find-count-of-variables-and-then/m-p/547953#M151900</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-02T16:29:41Z</dc:date>
    </item>
  </channel>
</rss>

