<?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 create variable with spefic format in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846230#M41783</link>
    <description>&lt;P&gt;You can't conditionally execute an ATTRIB or LENGTH statement as it is not an executable statement. It is good practice if you want to ensure that a variable always has a required type and length to define it in an ATTRIB statement regardless of whether it exists in your input datasets or not.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Nov 2022 21:46:06 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2022-11-24T21:46:06Z</dc:date>
    <item>
      <title>how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846222#M41776</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have checked on the web and I have found many way to check if a variable exist but how do we create new variable with specific format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What's the best way ?&lt;/P&gt;
&lt;P&gt;I have tried the length statement in both case when the variable exist and when the variable does not exist and I don't see any error or warning into the log.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But is there a better way to do that, ie. check if the variable exist and if not create it with a specific format.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question is how to do that&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class1 (drop=sex);
set sashelp.class;
run;

data class2;
set sashelp.class;
run;

data test;
length sex $ 1.;
set class1;
run;

data test2;
length sex $ 1.;
set class2;
run;

data test3;
set class2;
run;&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, 24 Nov 2022 20:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846222#M41776</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2022-11-24T20:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846223#M41777</link>
      <description>Format, or type (character/numeric) or length? You seem to be using the term interchangeably but they're all specific terms for SAS programming. I suspect you want type.&lt;BR /&gt;&lt;BR /&gt;What happens if the variable exists but isn't the right type?</description>
      <pubDate>Thu, 24 Nov 2022 20:49:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846223#M41777</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-11-24T20:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846224#M41778</link>
      <description>Always, ALWAYS, start a data step with an attrib statement describing each variable. Do not use the old means of handling it like length function. The attrib will create the variable for you exactly as you want it.</description>
      <pubDate>Thu, 24 Nov 2022 21:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846224#M41778</guid>
      <dc:creator>AlanC</dc:creator>
      <dc:date>2022-11-24T21:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846227#M41780</link>
      <description>Thank you very much for this precision.  What's about the best way to check if a variable exist and if not how do we integrate the attrib function information into the dataset?&lt;BR /&gt;</description>
      <pubDate>Thu, 24 Nov 2022 21:17:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846227#M41780</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2022-11-24T21:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846228#M41781</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13798"&gt;@AlanC&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Always, ALWAYS, start a data step with an attrib statement describing each variable. Do not use the old means of handling it like length function. The attrib will create the variable for you exactly as you want it.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The second sentence does not make any sense.&amp;nbsp; There is no difference in how the variable is defined (its type and its storage length) that is done differently with an ATTRIB statement than with the LENGTH statement.&amp;nbsp; Perhaps you meant to say that you personally prefer to use an ATTRIB statement with the LENGTH= option to define the variables instead of just using a LENGTH statement?&lt;/P&gt;</description>
      <pubDate>Thu, 24 Nov 2022 21:22:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846228#M41781</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-24T21:22:50Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846229#M41782</link>
      <description>The attrib, introduced later, handles all aspects for the pdv including length, format, informat, and label. Hope that clears it up.</description>
      <pubDate>Thu, 24 Nov 2022 21:39:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846229#M41782</guid>
      <dc:creator>AlanC</dc:creator>
      <dc:date>2022-11-24T21:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846230#M41783</link>
      <description>&lt;P&gt;You can't conditionally execute an ATTRIB or LENGTH statement as it is not an executable statement. It is good practice if you want to ensure that a variable always has a required type and length to define it in an ATTRIB statement regardless of whether it exists in your input datasets or not.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Nov 2022 21:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846230#M41783</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-11-24T21:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846393#M41786</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13798"&gt;@AlanC&lt;/a&gt;&amp;nbsp;The attrib statement will only assign a format if you explicitly use the format option so it's really only another syntax option in place of using the length, format, informat and label statements.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Nov 2022 00:41:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846393#M41786</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-11-26T00:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846397#M41787</link>
      <description>&lt;P&gt;Yes, I know. I think it was introduced in v6 but it simplified the variable creation a lot and initializes the PDV. It doesn't do anything different but it puts the variable info in one place and is easier to read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is statement on my Tips/Tricks on GitHub:&amp;nbsp;&lt;A href="https://github.com/savian-net/SasTipsTricks#attrib-statement" target="_blank" rel="noopener"&gt;savian-net/SasTipsTricks: Tips and tricks learned over 20+ years as a SAS/Microsoft consultant (github.com)&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I was a SAS consultant, I left the client's with a variation of that Tips/Tricks document...at every client. I 'think' it was best practice to use ATTRIB in SAS Consulting but not sure (too many years). Here is sample code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname temp 'x:\temp';

data temp.temp (drop=i);
   attrib AA1 label="Var1"
          AA2 label="Var2"
          AA3 label="Var3" length=$10 format=$7. informat=$9.
          AA4 label="Var4" length=$10
          AA5 label="Var5" length=$10
          AA6 label="Var6" length=$10
          AA7 label="Var7" length=$10
          AA8 label="Var8" length=$10
          AA9 label="Var9"
		  ;
   do i = 1 to 10;
      aa1 = 1 ;
	  aa2 = 2;
      aa3 = "Test_" || put(i,z4.);
	  aa4 = "Test2" ;
	  aa5 = "Test3" ;
	  aa6 = "Test4" ;
	  aa7 = "Test5" ;
	  aa8 = "Test6" ;
	  aa9 = 12 ;
 	  output ;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Nov 2022 00:56:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846397#M41787</guid>
      <dc:creator>AlanC</dc:creator>
      <dc:date>2022-11-26T00:56:55Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846398#M41788</link>
      <description>&lt;P&gt;So FORMAT has a specific meaning in SAS and it is not what your code is doing.&amp;nbsp; Your code is defining the variable.&amp;nbsp; It is not attaching a format to specify the way that you want SAS to display the values.&amp;nbsp; SAS has only two types of variables, floating point numbers or fixed length character strings.&amp;nbsp; So setting the LENGTH will define for SAS which type it is, and also how much storage space to use for it when writing the dataset to disk.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason why it might be useful to test if the variable already exists or not is to avoid the error that will happen if you try to define it with a different type.&amp;nbsp; For example try this program:&lt;/P&gt;
&lt;PRE&gt;498  data class1;
499    set sashelp.class;
500    length sex 8 age $3 ;
ERROR: Numeric length cannot be used with character variable Sex.
ERROR: Character length cannot be used with numeric variable Age.
501  run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.CLASS1 may be incomplete.  When this step was stopped there were 0 observations and 5 variables.
&lt;/PRE&gt;
&lt;P&gt;Now if you use a macro like&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/varexist.sas" target="_self"&gt;%varexist()&lt;/A&gt;&amp;nbsp;that can check if the variable exists and also its type then you can use macro logic to generate the proper code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if SEX is supposed to be character and it is numeric you might do something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %varexist(class,sex,type)=N %then %do;
data want;
  set class(rename=(sex=sexn));
  sex=put(sexn,1.);
run;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if SEX is supposed to be numeric and it is character you might do something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %varexist(class,sex,type)=C %then %do;
data want;
  set class(rename=(sex=sexc));
  sex=input(sexc,32.);
run;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Nov 2022 01:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846398#M41788</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-26T01:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846665#M41792</link>
      <description>Good afternoon,&lt;BR /&gt;&lt;BR /&gt;Yes you are right.  I should not use the term with specific format.  It is a bad translation from French to English.  In that case, I refer to type effectively</description>
      <pubDate>Mon, 28 Nov 2022 18:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846665#M41792</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2022-11-28T18:29:12Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846671#M41793</link>
      <description>Hello Reeza, &lt;BR /&gt;You are right.  I should not use the term Format for what I have in mind.  It is a bad translation from French to English.  And in SAS format is something else.   What I refer to is the type ex: character or numeric and the length as well.&lt;BR /&gt;&lt;BR /&gt;Moreover, again, you are right, what happen if it is the variable exist but if the type or the length is not good.</description>
      <pubDate>Mon, 28 Nov 2022 19:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846671#M41793</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2022-11-28T19:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846843#M41802</link>
      <description>I have tried the varexist macro function and it does not seems to works.</description>
      <pubDate>Tue, 29 Nov 2022 15:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846843#M41802</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2022-11-29T15:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846852#M41803</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76331"&gt;@alepage&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I have tried the varexist macro function and it does not seems to works.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Provide example code where it does not work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is example of how to retrieve and compile the macro and then use it to detect the variable type.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename varexist url "https://raw.githubusercontent.com/sasutils/macros/master/varexist.sas";
%include varexist;
%put NAME %varexist(sashelp.class,name,type);
%put AGE  %varexist(sashelp.class,age,type);
%put XXX  %varexist(sashelp.class,xxx,type);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;695  %put NAME %varexist(sashelp.class,name,type);
NAME C
696  %put AGE  %varexist(sashelp.class,age,type);
AGE  N
697  %put XXX  %varexist(sashelp.class,xxx,type);
XXX  0

&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Nov 2022 18:29:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846852#M41803</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-29T18:29:33Z</dc:date>
    </item>
    <item>
      <title>Re: how to create variable with spefic format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846917#M41804</link>
      <description>Thank you very much&lt;BR /&gt;</description>
      <pubDate>Tue, 29 Nov 2022 20:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/how-to-create-variable-with-spefic-format/m-p/846917#M41804</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2022-11-29T20:38:39Z</dc:date>
    </item>
  </channel>
</rss>

