<?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 Change the length of a character variable: proc sql modify in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Change-the-length-of-a-character-variable-proc-sql-modify/m-p/279975#M56511</link>
    <description>&lt;P&gt;Dear experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;on the basis of our explanation I am trying co change my data as it follows:&lt;/P&gt;
&lt;P&gt;1. increase the length of the variable:&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;alter table DC.input_analysis_res&lt;BR /&gt;modify Default_value char(20) format=$20. informat=$20.;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. insert the string "&lt;SPAN&gt;No_def_v&lt;/SPAN&gt;"&lt;BR /&gt;/* set unique value in case of default value missing */&lt;BR /&gt;data DC.input_analysis_res; set DC.input_analysis_res; if Default_value in ('',' ','-','.') then Default_value='No_def_v';run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is wrong with it? I got in the new varibale only the first two digits, i.e. "No" without the rest "&lt;SPAN&gt;_def_v&lt;/SPAN&gt;"&lt;/P&gt;</description>
    <pubDate>Wed, 08 Jun 2022 20:20:17 GMT</pubDate>
    <dc:creator>Sir_Highbury</dc:creator>
    <dc:date>2022-06-08T20:20:17Z</dc:date>
    <item>
      <title>Change the length of a character variable: proc sql modify</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-the-length-of-a-character-variable-proc-sql-modify/m-p/279975#M56511</link>
      <description>&lt;P&gt;Dear experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;on the basis of our explanation I am trying co change my data as it follows:&lt;/P&gt;
&lt;P&gt;1. increase the length of the variable:&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;alter table DC.input_analysis_res&lt;BR /&gt;modify Default_value char(20) format=$20. informat=$20.;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. insert the string "&lt;SPAN&gt;No_def_v&lt;/SPAN&gt;"&lt;BR /&gt;/* set unique value in case of default value missing */&lt;BR /&gt;data DC.input_analysis_res; set DC.input_analysis_res; if Default_value in ('',' ','-','.') then Default_value='No_def_v';run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is wrong with it? I got in the new varibale only the first two digits, i.e. "No" without the rest "&lt;SPAN&gt;_def_v&lt;/SPAN&gt;"&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2022 20:20:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-the-length-of-a-character-variable-proc-sql-modify/m-p/279975#M56511</guid>
      <dc:creator>Sir_Highbury</dc:creator>
      <dc:date>2022-06-08T20:20:17Z</dc:date>
    </item>
    <item>
      <title>Re: change the length of a charachter variable: proc sql modify</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-the-length-of-a-character-variable-proc-sql-modify/m-p/279993#M56518</link>
      <description>&lt;P&gt;Since your second step (DATA Step) recreates the original data, you could simply use a LENGTH statement before the SET statement to increase the length of a variable. To me your code looks correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try the code below for a sample program using SASHELP.CLASS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table newclass as 
    select * from sashelp.class
  ;
  alter table newClass
    modify name char(20)
  ;
quit;

data want;
  set newclass;

  if sex = "M" then do;
    name = catx(":", "CHG", name, sex, age);
  end;
run;

data want2;
  length name $ 20;
  set sashelp.class;

  if sex = "M" then do;
    name = catx(":", "CHG", name, sex, age);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;
&lt;P&gt;Editor's note:&lt;BR /&gt;As suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, a character variable format can cause only a few of the available characters to display, even if the length has been changed successfully. Always check the metadata for actual length after modifying a table. For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.cars_data_step_length
	  work.cars_sql_modify;
   /* Use LENGTH before SET to change length */
   length Make $200;
	set sashelp.cars (obs=5);
run;

proc sql;
/* Use ALTER TABLE / MODIFY to change length */
alter table work.cars_sql_modify
  modify Make char(100)
;
title "Final Variable Lengths ";
select catx('.',libname,Memname) as Table, Name, Type, Length 
   from dictionary.columns 
   where memname like 'CARS%'
	   and libname in ('SASHELP','WORK')
	   and Name='Make';
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For information about removing unwanted formats, resizing, and renaming table columns, see the SAS Tutorial "&lt;A href="https://youtu.be/0mCV_D65Vqo" target="_self"&gt;Resize, Rename and Reformat Data with SAS Macros&lt;/A&gt;".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2022 19:23:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-the-length-of-a-character-variable-proc-sql-modify/m-p/279993#M56518</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2022-07-29T19:23:41Z</dc:date>
    </item>
    <item>
      <title>Re: change the length of a charachter variable: proc sql modify</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-the-length-of-a-character-variable-proc-sql-modify/m-p/280037#M56527</link>
      <description>&lt;P&gt;RTM - this note is shaded in grey.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can't change the length of a variable using the modify statement. You'll have to use a length statement before the Set statement as identified already.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2016 17:41:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-the-length-of-a-character-variable-proc-sql-modify/m-p/280037#M56527</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-06-24T17:41:20Z</dc:date>
    </item>
    <item>
      <title>Re: change the length of a charachter variable: proc sql modify</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-the-length-of-a-character-variable-proc-sql-modify/m-p/280125#M56560</link>
      <description>&lt;PRE&gt;
It is really weird . Maybe your format is still $2. .Change it in data step and see what happened.
The following code worked for me .



data have;
 set sashelp.class;
run;

proc sql;
alter table have
 modify sex char(10);
quit;

data have;
 set have;
 if sex='F' then sex='XXXXXXX';
 format sex $20.;
run;

proc print;run;





OUTPUT:

Obs	Name	Sex	Age	Height	Weight
1	Alfred	M	14	69.0	112.5
2	Alice	XXXXXXX	13	56.5	84.0
3	Barbara	XXXXXXX	13	65.3	98.0
4	Carol	XXXXXXX	14	62.8	102.5
5	Henry	M	14	63.5	102.5
6	James	M	12	57.3	83.0
7	Jane	XXXXXXX	12	59.8	84.5
8	Janet	XXXXXXX	15	62.5	112.5
9	Jeffrey	M	13	62.5	84.0
10	John	M	12	59.0	99.5
11	Joyce	XXXXXXX	11	51.3	50.5
12	Judy	XXXXXXX	14	64.3	90.0
13	Louise	XXXXXXX	12	56.3	77.0
14	Mary	XXXXXXX	15	66.5	112.0
15	Philip	M	16	72.0	150.0
16	Robert	M	12	64.8	128.0
17	Ronald	M	15	67.0	133.0
18	Thomas	M	11	57.5	85.0
19	William	M	15	66.5	112.0

&lt;/PRE&gt;</description>
      <pubDate>Sat, 25 Jun 2016 08:49:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-the-length-of-a-character-variable-proc-sql-modify/m-p/280125#M56560</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-25T08:49:49Z</dc:date>
    </item>
  </channel>
</rss>

