<?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 judge a variable has custom format or not? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956032#M373340</link>
    <description>&lt;P&gt;A few issues with your code, the main one being that it only detects custom formats that have been defined.&lt;/P&gt;
&lt;P&gt;This works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro var_hascustfmt(ds, var);
  %local dsid result;
  %let dsid=%sysfunc(open(&amp;amp;ds.));
  %if &amp;amp;dsid. %then %do;
    %let fmtname=%sysfunc(varfmt(&amp;amp;dsid.,%sysfunc(varnum(&amp;amp;dsid.,&amp;amp;var.))));                            %* Get format name from source table;
    %let fmtname=%sysfunc(prxchange(s/[\d\.]*$//, 1, &amp;amp;fmtname.));                                    %* Remove format length &amp;amp; final dot ;
    %let dsid   =%sysfunc(close(&amp;amp;dsid.));                                                            %* Close source table               ;
    %if %length(&amp;amp;fmtname.) %then %do;                                                                %* Var format found, vet it         ;
      %let dsid  =%sysfunc(open(SASHELP.VFORMAT(where=(FMTNAME="&amp;amp;fmtname" &amp;amp; SOURCE in ('U','B'))))); %* See if format is standard        ;
      %let result=%sysfunc(ifc(%sysfunc(attrn(&amp;amp;dsid.,nlobsf)), 0, 1));                               %* Not standard =&amp;gt; set flag to 1    ;
      %let dsid  =%sysfunc(close(&amp;amp;dsid.));                                                           %* Close format table               ;
    %end;                                                                                            %* End of 'var format found'        ;
    %else %let result=0;                                                                             %* No format =&amp;gt; set flag to 0       ;
  %end;                                                                                              %* End of format search             ;      
  %else %let result=-1;                                                                              %* No data set =&amp;gt; set flag to -1    ;    
&amp;amp;result.
%mend;

proc format;
  value $gender
  'F'='Female'
  'M'='Male'
  ;
run;

data HAVE;
  SUBJECT  =1;
  GENDER   ='F';
  BIRTHDATE='01jan2000'd;
  WEIGHT   =59;
  format SUBJECT z13.2 GENDER $gender. BIRTHDATE e8601da. AGE sssss.;
run;

%put %var_hascustfmt(HAVE,SUBJECT);    %* z13.2       ;
%put %var_hascustfmt(HAVE,GENDER);     %* Custom      ;
%put %var_hascustfmt(HAVE,BIRTHDATE);  %* e8601da.    ;
%put %var_hascustfmt(HAVE,WEIGHT);     %* Unformatted ;
%put %var_hascustfmt(HAVE,AGE);        %* Undefined   ;
%put %var_hascustfmt(HAVEXX,WEIGHT);&amp;nbsp;&amp;nbsp;&amp;nbsp;%*&amp;nbsp;No&amp;nbsp;data&amp;nbsp;set&amp;nbsp;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This should be further improved by checking that the variable exists.&lt;/P&gt;</description>
    <pubDate>Tue, 14 Jan 2025 10:57:35 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2025-01-14T10:57:35Z</dc:date>
    <item>
      <title>How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956023#M373336</link>
      <description>&lt;P&gt;I want to develop a macro, named &lt;CODE class=" language-sas"&gt;%var_hascustfmt&lt;/CODE&gt;, to judge a variable has custom format or not. It is designed to return 1 or 0, for&amp;nbsp;variable has custom format or not.&lt;/P&gt;
&lt;P&gt;This is the source code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro var_hascustfmt(data,var);
%local did did2 result;
%let did=%sysfunc(open(&amp;amp;data.));
%if &amp;amp;did.^=0 %then %do;
  %let fmtname=%sysfunc(varfmt(&amp;amp;did.,%sysfunc(varnum(&amp;amp;did.,&amp;amp;var.))));
  %let did=%sysfunc(close(&amp;amp;did.));

  %let result=0;
  %if &amp;amp;fmtname.^= %then %do;
    %let did2=%sysfunc(open(sashelp.vcformat(where=(trim(fmtname)||'.'="&amp;amp;fmtname."))));
    %if %sysfunc(attrn(&amp;amp;did2.,nlobsf))^=0 %then %let result=1;
    %let did2=%sysfunc(close(&amp;amp;did2.));
  %end;

%end;
&amp;amp;result.
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Test data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  value $gender
  'F'='Female'
  'M'='Male'
  ;
run;

data have;
  subject=1;
  gender='F';
  birthdate='01jan2000'd;
  weight=59;
  format subject z3. gender $gender. birthdate e8601da.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Test code and result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put %var_hascustfmt(have,subject);&lt;BR /&gt;0
%put %var_hascustfmt(have,gender);&lt;BR /&gt;1
%put %var_hascustfmt(have,birthdate);&lt;BR /&gt;0
%put %var_hascustfmt(have,weight);&lt;BR /&gt;0&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So far, it works good. As you can see, the macro return value by query name of variable's format in sashelp.vcformat, which is a view of dictionary.formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question is:&lt;/P&gt;
&lt;P&gt;1. Is this way always reliable? What does sashelp.vcformt stores in official definition?&lt;/P&gt;
&lt;P&gt;2. Is there any other implements? Access to dictionary table is not always fast enough.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 08:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956023#M373336</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2025-01-14T08:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956032#M373340</link>
      <description>&lt;P&gt;A few issues with your code, the main one being that it only detects custom formats that have been defined.&lt;/P&gt;
&lt;P&gt;This works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro var_hascustfmt(ds, var);
  %local dsid result;
  %let dsid=%sysfunc(open(&amp;amp;ds.));
  %if &amp;amp;dsid. %then %do;
    %let fmtname=%sysfunc(varfmt(&amp;amp;dsid.,%sysfunc(varnum(&amp;amp;dsid.,&amp;amp;var.))));                            %* Get format name from source table;
    %let fmtname=%sysfunc(prxchange(s/[\d\.]*$//, 1, &amp;amp;fmtname.));                                    %* Remove format length &amp;amp; final dot ;
    %let dsid   =%sysfunc(close(&amp;amp;dsid.));                                                            %* Close source table               ;
    %if %length(&amp;amp;fmtname.) %then %do;                                                                %* Var format found, vet it         ;
      %let dsid  =%sysfunc(open(SASHELP.VFORMAT(where=(FMTNAME="&amp;amp;fmtname" &amp;amp; SOURCE in ('U','B'))))); %* See if format is standard        ;
      %let result=%sysfunc(ifc(%sysfunc(attrn(&amp;amp;dsid.,nlobsf)), 0, 1));                               %* Not standard =&amp;gt; set flag to 1    ;
      %let dsid  =%sysfunc(close(&amp;amp;dsid.));                                                           %* Close format table               ;
    %end;                                                                                            %* End of 'var format found'        ;
    %else %let result=0;                                                                             %* No format =&amp;gt; set flag to 0       ;
  %end;                                                                                              %* End of format search             ;      
  %else %let result=-1;                                                                              %* No data set =&amp;gt; set flag to -1    ;    
&amp;amp;result.
%mend;

proc format;
  value $gender
  'F'='Female'
  'M'='Male'
  ;
run;

data HAVE;
  SUBJECT  =1;
  GENDER   ='F';
  BIRTHDATE='01jan2000'd;
  WEIGHT   =59;
  format SUBJECT z13.2 GENDER $gender. BIRTHDATE e8601da. AGE sssss.;
run;

%put %var_hascustfmt(HAVE,SUBJECT);    %* z13.2       ;
%put %var_hascustfmt(HAVE,GENDER);     %* Custom      ;
%put %var_hascustfmt(HAVE,BIRTHDATE);  %* e8601da.    ;
%put %var_hascustfmt(HAVE,WEIGHT);     %* Unformatted ;
%put %var_hascustfmt(HAVE,AGE);        %* Undefined   ;
%put %var_hascustfmt(HAVEXX,WEIGHT);&amp;nbsp;&amp;nbsp;&amp;nbsp;%*&amp;nbsp;No&amp;nbsp;data&amp;nbsp;set&amp;nbsp;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This should be further improved by checking that the variable exists.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 10:57:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956032#M373340</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2025-01-14T10:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956037#M373342</link>
      <description>&lt;P&gt;If you want to speed up the query, copy the table (only 2 columns and only U and B values) the first time you run the macro, and then query the copy.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 10:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956037#M373342</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2025-01-14T10:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956148#M373382</link>
      <description>Thank you, what does U and B means?</description>
      <pubDate>Wed, 15 Jan 2025 02:34:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956148#M373382</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2025-01-15T02:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956154#M373386</link>
      <description>&lt;P&gt;Wouldn't it be easier to just generate some SAS code to do that work instead of all of those %SYSFUNC() calls?&lt;/P&gt;
&lt;P&gt;What value does it add to jump through so many hoops make it simulate a function?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Plus if you generate SAS code instead you can do things like use the FORMAT variable generated by PROC CONTENTS so you don't have to work to strip off the width and period and decimal place specifications.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 04:20:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956154#M373386</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-15T04:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956157#M373389</link>
      <description>U and B just show how the format is packaged internally in SAS. B stands for Base iirc. See here for a related discussion &lt;A href="https://support.sas.com/resources/papers/proceedings17/SAS0209-2017.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings17/SAS0209-2017.pdf&lt;/A&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 15 Jan 2025 08:10:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956157#M373389</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2025-01-15T08:10:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956159#M373390</link>
      <description>SAS code shows in the log and restrict how the function can be used. There's nothing wrong with using %sysfunc, especially for such a tiny block of code.</description>
      <pubDate>Wed, 15 Jan 2025 08:16:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956159#M373390</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2025-01-15T08:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956166#M373391</link>
      <description>&lt;P&gt;Yes, you are right, I have several peices of code to achieve the similiar thing now:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Method 1;
data _null_;
  if 0 then set have(keep=subject);
  _vformatn_=vformatn(subject);

  set sashelp.vcformat;
  if fmtname=_vformatn_ then call symputx('customformatexist',1);
run;

*Method 2;
proc contents data=have out=tab1(where=(name='subject')) noprint;
run;

proc sql noprint;
  select 1 into :customformatexist trimmed from tab1 
  where format in (select fmtname from dictionary.formats where source='C');
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;They all work.&lt;/P&gt;
&lt;P&gt;Thank you for inspiring me, I decide to use the proc content way in another macro.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 08:49:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956166#M373391</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2025-01-15T08:49:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956171#M373394</link>
      <description>&lt;P&gt;1. You're devaluing your function by using SAS code.&lt;/P&gt;
&lt;P&gt;For example, you can no longer run&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  VAR='WEIGHT';
  if resolve('%var_hascustfmt(HAVEXX,'||VAR||')') = '-1' then putlog 'No data set!';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There are ways around this, but why not use what works?&lt;/P&gt;
&lt;P&gt;2. Again, your code will not work if the custom formats have not been defined.&lt;/P&gt;
&lt;P&gt;Test against SAS format names, not against custom format names.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 10:26:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956171#M373394</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2025-01-15T10:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956222#M373411</link>
      <description>&lt;P&gt;Another hint for code like that last one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select 1 into :customformatexist trimmed from tab1 
  where format in (select fmtname from dictionary.formats where source='C');
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Make sure to initialize the macro variable since when the no observations meet the WHERE condition the macro variable is not generated.&lt;/P&gt;
&lt;P&gt;Since macro variables are just text just use '1' instead of 1 in the query.&amp;nbsp; Then you won't need the TRIMMED keyword since nothing needs trimming.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
%let customformatexist = 0;
select '1' into :customformatexist 
  from tab1 
  where format in (select fmtname from dictionary.formats where source='C')
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2025 16:58:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956222#M373411</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-15T16:58:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956272#M373433</link>
      <description>Many thanks to your second hint, will use it in real work.&lt;BR /&gt;For the first one, I actually use functionality of %var_hascustfmt() in another big macro, so parameters 'data' and 'var' are validated.</description>
      <pubDate>Thu, 16 Jan 2025 01:53:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956272#M373433</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2025-01-16T01:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to judge a variable has custom format or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956273#M373434</link>
      <description>Nice trick.</description>
      <pubDate>Thu, 16 Jan 2025 01:55:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-judge-a-variable-has-custom-format-or-not/m-p/956273#M373434</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2025-01-16T01:55:26Z</dc:date>
    </item>
  </channel>
</rss>

