<?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: Convert all numeric to char with apply the format of each var in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875930#M346097</link>
    <description>In this example it works 100%  but in real word there were some columns that the values are not thr formatted  values in the  wanted data set</description>
    <pubDate>Tue, 16 May 2023 07:10:28 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2023-05-16T07:10:28Z</dc:date>
    <item>
      <title>Convert all numeric to char with apply the format of each var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875666#M345995</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;In my data set have numeric variables and char variables.&lt;/P&gt;
&lt;P&gt;Some of numeric variables have user defined format that applied to them.&lt;/P&gt;
&lt;P&gt;Some of numeric variables have regular numeric format best8.&lt;/P&gt;
&lt;P&gt;My question:&lt;/P&gt;
&lt;P&gt;I want to convert all numeric variables to char variables using the following rule:&lt;/P&gt;
&lt;P&gt;If&amp;nbsp; numeric var has user defined format then the conversion should be done with PUT(numeric_Vat, user_Fmt)&lt;/P&gt;
&lt;P&gt;If&amp;nbsp;&amp;nbsp;numeric var has regular numeric format (best8.) then&amp;nbsp; conversion should be done with PUT(numeric_Vat, best.)&lt;/P&gt;
&lt;P&gt;Please note that there are different user define formats applied on different numeric vars&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;What is the way to do it automatically that sas will convert the char vars by the criteria I mentioned?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value X_Fmt
1='A'
2='B'
;
value Y_Fmt
1='Y'
2='N'
;
Run;

data have;
format x X_Fmt.  y Y_Fmt.;
input ID  X Y W Z;
cards;
1 1 1 1 2
2 1 2 2 2
3 1 1 3 2
4 2 2 3 2
5 2 1 2 1
;
Run;


data want(Drop=X Y W Z rename=(X_=X  Y_=Y  W_=W  Z_=Z));
set have;
X_=put(X,x_fmt.);
Y_=put(Y,y_Fmt.);
W_=put(W,best.);
Z_=put(Z,best.);
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 May 2023 13:13:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875666#M345995</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-05-14T13:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric to char with apply the format of each var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875670#M345997</link>
      <description>&lt;P&gt;Use the VVALUE() function to get the formatted value of a variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x_=vvalue(x);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or just write the data to text file and read it back in.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;fliename dummy temp;
data _null_;
  set sashelp.class;
  file dummy dsd ;
  put (_all_) (+0);
run;

data want;
  infile dummy dsd truncover;
  input name :$20. sex :$8. (age height weight) (:$12.) ;
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 May 2023 13:32:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875670#M345997</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-14T13:32:20Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric to char with apply the format of each var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875685#M346006</link>
      <description>&lt;P&gt;You can do it with PROC EXPORT/PROC IMPORT, if you use the EFI_ALLCHARS macro variable to control how PROC IMPORT reads the file.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename dummy temp;
proc export data=have file=dummy dbms=csv; run;
%let efi_allchars=YES;
proc import file=dummy dbms=csv out=want replace; 
  guessingrows=max;
run;
%symdel efi_allchars;

proc contents data=want varnum; run;
proc print data=want; run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is the HAVE variable definitions:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1684082878212.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83963i8B39AA31706FA38C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1684082878212.png" alt="Tom_1-1684082878212.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Here is the resulting WANT dataset.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1684082812944.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83962i6DD5DBAA1360B1F7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1684082812944.png" alt="Tom_0-1684082812944.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2023 16:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875685#M346006</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-14T16:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric to char with apply the format of each var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875695#M346010</link>
      <description>&lt;P&gt;Perfect solution and thank you.&lt;/P&gt;
&lt;P&gt;This is perfect solution because it is dynamic (Work on any set of variables and user shouldn't type the variables names)&lt;/P&gt;
&lt;P&gt;and also the lengths of the resulted variables are fitted to the values .&lt;/P&gt;
&lt;P&gt;I have some questions please:&lt;/P&gt;
&lt;P&gt;1-Export Step-&lt;/P&gt;
&lt;P&gt;Where is the dummy file created?&lt;/P&gt;
&lt;P&gt;Where can I see it?&lt;/P&gt;
&lt;P&gt;Is it CSV file?&lt;/P&gt;
&lt;P&gt;Why did you choose to export to CSV file?&lt;/P&gt;
&lt;P&gt;Is it essential to use "Dummy" word in file statement or can choose any name?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename dummy  temp;
proc export  data=have file=dummy  dsbms=csv;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2-Import process&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let efi_allchars=YES;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Does it mean that all varaibles will be defined has char during import process?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;guessingrows=max&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Does it mean that SAS use max number of observations in order to determine column length?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2023 20:26:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875695#M346010</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-05-14T20:26:15Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric to char with apply the format of each var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875705#M346013</link>
      <description>&lt;P&gt;DUMMY is just the fileref value I picked. You can use any fileref you want (as long as it does not conflict with some other fileref you need).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TEMP engine makes the file in the WORK directory so you don't have to worry about giving it a name or about deleting it.&amp;nbsp; If you want to keep it you could make up a permanent filename instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to look at if it you can. Just use the fileref in another program, such as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* dump first five lines to the log ;
data _null_;
  infile dummy obs=5;
  input;
  list;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used a CSV file because the EFI_ALLCHARS setting only applies to reading of delimited text file.&amp;nbsp; It does not matter whether you use comma as the delimiter or some other character.&amp;nbsp; Just be consistent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The GUESSINGROWS=MAX will insure that it does not truncate the data.&amp;nbsp; (Note that I think PROC IMPORT does not count properly when the values contain the delimiters.&amp;nbsp; It counts the quotes that are added around the values that contain delimiters, which can cause it to make the variables slightly longer than they need to be.)&lt;/P&gt;
&lt;P&gt;Example where PROC IMPORT makes the variable longer than it has to be:&lt;/P&gt;
&lt;LI-SPOILER&gt;Variable A only needs to be 3 bytes long.&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename dummy temp;
options parmcards=dummy;
parmcards;
id,a,b,c
Alice,"1,2",XX,2
Betty,,,3
Carol,4,5,
;

%let efi_allchars=YES;
proc import file=dummy dbms=csv out=want replace;
  guessingrows=max;
run;
%symdel efi_allchars;

proc contents data=want varnum;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1684104055884.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83967iE2C57BD20AB77C26/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1684104055884.png" alt="Tom_0-1684104055884.png" /&gt;&lt;/span&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BR /&gt;&lt;BR /&gt;&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the EFI_ALLCHARS setting has a bug.&amp;nbsp; It can get fooled into making a column as numeric if any of the values happen to just be a period. That can only happen with a character variable since a missing numeric value would have been written as nothing at all.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example of '.' breaking EFI_ALLCHARS=YES setting.&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename dummy temp;
options parmcards=dummy;
parmcards;
id,a,b,c
Alice,.,XX,2
Betty,,.,3
Carol,4,5,
;

%let efi_allchars=YES;
proc import file=dummy dbms=csv out=want replace;
  guessingrows=max;
run;
%symdel efi_allchars;

proc contents data=want varnum;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2023 22:41:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875705#M346013</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-14T22:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all numeric to char with apply the format of each var</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875930#M346097</link>
      <description>In this example it works 100%  but in real word there were some columns that the values are not thr formatted  values in the  wanted data set</description>
      <pubDate>Tue, 16 May 2023 07:10:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-numeric-to-char-with-apply-the-format-of-each-var/m-p/875930#M346097</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-05-16T07:10:28Z</dc:date>
    </item>
  </channel>
</rss>

