<?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: Extracting all variables which has length less than 32 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875327#M345858</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441935"&gt;@nxmogil&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I want to create a dataset which has variables.having a length of 32&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A data set with a LIST of variable names that have a length of less than 32? Or a data set with the variables themselves and the values? Please clarify.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you want a length of 32, but originally you said less than 32. Please clarify.&lt;/P&gt;</description>
    <pubDate>Thu, 11 May 2023 18:46:46 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-05-11T18:46:46Z</dc:date>
    <item>
      <title>Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875241#M345819</link>
      <description>&lt;P&gt;Hi Can any one please help me with the code to extract all variables only having a length less than 32 from SQL server using SAS&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 14:46:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875241#M345819</guid>
      <dc:creator>nxmogil</dc:creator>
      <dc:date>2023-05-11T14:46:17Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875245#M345822</link>
      <description>&lt;P&gt;What do you mean by "extracting"? Do you want just a list of those variable names which have length less than 32, or do you want to create a data set(s) with those variables? Or do you want something else?&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 14:57:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875245#M345822</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-11T14:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875257#M345829</link>
      <description>&lt;P&gt;First connect to the SQL server database.&lt;/P&gt;
&lt;P&gt;Let's assume you have done that by defining a libref named SQLSRV.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second ask SQL Server to tell you names of the columns in the table.&lt;/P&gt;
&lt;P&gt;So if this article&amp;nbsp;&lt;A href="https://www.mytecbits.com/microsoft/sql-server/list-of-column-names" target="_blank"&gt;https://www.mytecbits.com/microsoft/sql-server/list-of-column-names&lt;/A&gt;&amp;nbsp;is right the code might look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
connect using SQLSRV ;
create table columns as 
select * from connection to SQLSRV 
(SELECT COLUMN_NAME, ORDINAL_POSITION, DATA_TYPE
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_NAME = 'Orders'
  ORDER BY 2
);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have that you should be able to answer your question.&lt;/P&gt;
&lt;P&gt;I would help but I don't understand what your actual question is.&lt;/P&gt;
&lt;P&gt;If you want to know if the length of the NAME of variable is less than 32 bytes then you can use the LENGTH() function on the COLUMN_NAME variable.&lt;/P&gt;
&lt;P&gt;If you want to know if the variable is defined to be able to store values that have a maximum length of less than 32 then you probably need to make sense of the values of the DATA_TYPE variable.&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 15:36:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875257#M345829</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-11T15:36:44Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875260#M345832</link>
      <description>I want to create a dataset which has variables.having a length of 32</description>
      <pubDate>Thu, 11 May 2023 15:43:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875260#M345832</guid>
      <dc:creator>nxmogil</dc:creator>
      <dc:date>2023-05-11T15:43:09Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875262#M345834</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441935"&gt;@nxmogil&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I want to create a dataset which has variables.having a length of 32&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This data step will create a dataset with three variable. All of which are defined as character with a length of 32.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length var1 varA var97 $32;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Is that what you are trying to do?&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 15:45:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875262#M345834</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-11T15:45:13Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875276#M345839</link>
      <description>Actually my dataset in SQL server has few variables which has more than 32 character's length, I just want to extract only the ones which has less than 32 character length</description>
      <pubDate>Thu, 11 May 2023 16:15:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875276#M345839</guid>
      <dc:creator>nxmogil</dc:creator>
      <dc:date>2023-05-11T16:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875279#M345840</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441935"&gt;@nxmogil&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Actually my dataset in SQL server has few variables which has more than 32 character's length, I just want to extract only the ones which has less than 32 character length&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Are you talking about the length of the NAME of the variable?&amp;nbsp; SAS variable names are limited to 32 characters.&lt;/P&gt;
&lt;P&gt;Are you talking about the defined maximum storage length for character variables?&amp;nbsp; SAS uses only FIXED LENGTH variables but SQL Server probably has many other types of character variables.&amp;nbsp; Do you know how to ask SQL Server how the variable is defined?&lt;/P&gt;
&lt;P&gt;Are you taling about the length of the values stored in the variables?&amp;nbsp; Are you asking only move the observations where the values of the selected variables has a length that is less than 32 bytes?&amp;nbsp; When talking about the length of character strings you need to be clear whether the strings are using a single byte encoding, like LATIN1, or if they are using a multi-byte encoding like UTF-8.&amp;nbsp; With multi-byte encodings a variable with a maximum length of 32 might not be able to store 32 characters is any of the characters require multiple bytes to be stored.&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 16:27:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875279#M345840</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-11T16:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875284#M345842</link>
      <description>&lt;P&gt;I'm not sure if it able to help you by doing this.&lt;/P&gt;&lt;P&gt;If you don't mind filter out all variables that have lenght &amp;lt;=32 and do Copy &amp;amp; Paste to select clause in Proc SQL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
describe table dictionary.columns; /*all variables in output are labels, you need to find out column name for subsequent action*/
	select *,
	length(name) as Column_Length /* 'name' is label for 'Column Name' */
	from dictionary.columns /* using dictionary to check summary of your tables/dataset */
	where libname = "SASHELP" and Memname = "AACOMP" and calculated Column_Length &amp;lt;=5;&amp;nbsp;/*&amp;nbsp;Here&amp;nbsp;is&amp;nbsp;the&amp;nbsp;sample&amp;nbsp;i&amp;nbsp;used,&amp;nbsp;you&amp;nbsp;may&amp;nbsp;refer&amp;nbsp;to&amp;nbsp;your&amp;nbsp;dataset&amp;nbsp;by&amp;nbsp;changing&amp;nbsp;value&amp;nbsp;in&amp;nbsp;Libname,&amp;nbsp;Memname&amp;nbsp;and&amp;nbsp;Length_column&amp;nbsp;*/&lt;/CODE&gt;&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>Thu, 11 May 2023 16:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875284#M345842</guid>
      <dc:creator>ChrisWoo</dc:creator>
      <dc:date>2023-05-11T16:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875288#M345844</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does a 'column length' variable exist in the DB? like: &lt;STRONG&gt;select COLUMN_NAME, DATA_TYPE, &lt;U&gt;COLUMN_LENGTH&lt;/U&gt;&lt;/STRONG&gt;&lt;BR /&gt;Not familiar with sql.server, just assuming if any variable exist that stores column length in it then WHERE clause could eventually pick up the required variables based on the length criteria as well.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A simple example from sas library columns:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table Columns_lt32_in_all_datasets as
		select distinct name, length, memname
			from dictionary.columns
		where libname eq 'SASHELP' and memname like 'A%' and length lt 32
	order by 2;
quit; 
proc print;run; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 16:55:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875288#M345844</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-05-11T16:55:36Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875290#M345846</link>
      <description>Looks like you need the variable names, that have less than 32 char..&lt;BR /&gt;Didn't you try the solution by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, using the filter WHERE length(Column_Name) lt 32?</description>
      <pubDate>Thu, 11 May 2023 17:03:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875290#M345846</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-05-11T17:03:41Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875298#M345849</link>
      <description>&lt;P&gt;Thank you all for your responses.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have written a query like this (nl is a library connecting to SQL Server)&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select * from nl.contracts;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;Then it thrown me an error &lt;STRONG&gt;variable ABC is not found&lt;/STRONG&gt;. When Googled it, found that ABC variable is more than 32 character length. So then thought in searching a code which can pull only the variables having less than 32 character from a table in SQL Server&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 17:21:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875298#M345849</guid>
      <dc:creator>nxmogil</dc:creator>
      <dc:date>2023-05-11T17:21:35Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875306#M345852</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441935"&gt;@nxmogil&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you all for your responses.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have written a query like this (nl is a library connecting to SQL Server)&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;select * from nl.contracts;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;Then it thrown me an error &lt;STRONG&gt;variable ABC is not found&lt;/STRONG&gt;. When Googled it, found that ABC variable is more than 32 character length. So then thought in searching a code which can pull only the variables having less than 32 character from a table in SQL Server&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A variable having a length greater than 32 would not cause any trouble.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that your actual issue is that the &lt;STRONG&gt;NAME&lt;/STRONG&gt; of the variable is longer than 32.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So instead of having trouble with a variable with a name like ABC you probably had trouble with a variable with a name like A_LIKE_TO_USE_THE_VARIABLE_DESCRIPTION_AS THE_NAME_INSTEAD_OF_USING_A_NAME_AS_THE_NAME.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don;t use SQL Server so you will have to test if the code I found on-line actually works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if it does you could use it to generate code.&amp;nbsp; First get the list of variable names into a SAS dataset you can work with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
connect using NL;
create table columns as 
select * from connection to NL
(SELECT COLUMN_NAME, ORDINAL_POSITION, DATA_TYPE
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE upper(TABLE_NAME) = 'CONTRACTS'
  ORDER BY 2
);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if the number of variables is small you could just put the list of names into a macro variable. (A macro variable can hold 64K bytes).&amp;nbsp; And then use the variable list with the KEEP= dataset option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select nliteral(column_name) 
  into :name_list separated by ' '
  from columns
  where length(column_name) &amp;lt;= 32
;
quit;

data want;
  set nl.contracts(keep=&amp;amp;name_list);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the number of variables is large then it is probably easier to use a data step to write the code to a file and then use %INCLUDE to run it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example you could do something like this to generate code that would let you get ALL of the variables, even the ones with names that are too long for SAS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* generate short names and label values to use in code generation ;
data columns;
  set columns;
  length sasname $32 nliteral $60 label $255 ;
  label=column_name;
  if length(column_name) &amp;lt;= 32 then sasname=column_name;
  else sasname=cats('VAR_',ordinal_position);
  if label = sasname then label="' '";
  else label=quote(trim(label),"'") ;
  nliteral=nliteral(sasname);
run;

* Write the SAS part of the SELECT ;
filename code temp;
data _null_;
  file code;
  set columns ;
  if _n_=1 then put 'create table want as select ' / ' ' @;
  else put ',' @;
  put nliteral 'label=' label;
run;

* Append the SQL Server part of the select ;
data _null_;
  file code mod;
  set columns end=eof;
  if _n_=1 then put 'from connection to NL'
         / '(salect ' @;
  else put '      , ' @;
  put '[' column_name ']' @ ;
  if sasname ne column_name then put 'as ' sasname ;
  else put ;
  if eof then put ' from [CONTRACTS]' / ');' ;
run;

* Run the generated code inside PROC SQL step;
proc sql;
  connect using NL ;
%include code / source2;
quit;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that you get something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select
  'short name'n  label=' '
, VAR_2 label='This description is too long to be used as a variable name'
from connection to NL
(select [short name]
      , [This description is too long to be used as a variable name] as VAR_2
  from CONTACTS
);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 22:42:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875306#M345852</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-11T22:42:50Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875327#M345858</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441935"&gt;@nxmogil&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I want to create a dataset which has variables.having a length of 32&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A data set with a LIST of variable names that have a length of less than 32? Or a data set with the variables themselves and the values? Please clarify.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you want a length of 32, but originally you said less than 32. Please clarify.&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 18:46:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875327#M345858</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-11T18:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting all variables which has length less than 32</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875338#M345865</link>
      <description>&lt;P&gt;Thank you somuch&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 20:13:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-all-variables-which-has-length-less-than-32/m-p/875338#M345865</guid>
      <dc:creator>nxmogil</dc:creator>
      <dc:date>2023-05-11T20:13:36Z</dc:date>
    </item>
  </channel>
</rss>

