BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BarryDeCicco
Obsidian | Level 7
The problem with using the length statement before setting the data set is that the modified variable will be the first variable in the new data set. The SQL statement can alter variables without changing their order.
SASJedi
SAS Super FREQ

This is a perfect opportunity to leverage some PROC SQL 🙂  Assuming your imported data set is "lib.have" and the name of the character variable you want to modify is "TextVar", this should work for you:

 
proc sql;
alter table lib.have
  modify TextVar char(20) format=$20. informat=$20.;
quit;
Check out my Jedi SAS Tricks for SAS Users
Tom
Super User Tom
Super User

This is one of my pet peeves with SAS's implementation of importing data from databases.  They always attach both a format and an informat to character variables.  As you have seen this can cause a number of headaches when you start combining that data with other datasets.

In general permanently attaching the $ format to a character variable adds nothing of value.

The only way I know to remove these is to use the FORMAT (or INFORMAT) statement with a variable list, but without a format.

data sasfile ;

   set dbfile ;

  format _character_ ;

run;

SASJedi
SAS Super FREQ

You can use PROC DATASETS to modify variable characteristics, including formats.  Here is some sample code to play with:

 
data test;
  set sashelp.class;
  format Name $8. weight z5.1;
run;
title "Formatted values";
proc print; run;

/*Get a list of variable names in a macro variable*/
proc sql noprint;
select Name into :Varnames separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='TEST';
quit;

/*Remove formats from all the variables*/
proc datasets library=work nolist;
  modify test;
  format &varnames;
quit;
title "After formats removed";
proc print; run;
Check out my Jedi SAS Tricks for SAS Users
Ksharp
Super User

SASJedi.

There is no need to use dictionary table any more , directly use variable list _all_;

data test;

  set sashelp.class;

  format Name $8. weight z5.1;

run;

title "Formatted values";

proc print; run;

/*Remove formats from all the variables*/

proc datasets library=work nolist;

  modify test;

  format _all_;

quit;

title "After formats removed";

proc print; run;

Ksharp

Sir_Highbury
Quartz | Level 8

Dear experts,

 

on the basis of our explanation I am trying co change my data as it follows:

1. increase the length of the variable:

proc sql;
alter table DC.input_analysis_res
modify Default_value char(20) format=$20. informat=$20.;
quit;

 

2. insert the string "No_def_v"
/* set unique value in case of default value missing */
data DC.input_analysis_res; set DC.input_analysis_res; if Default_value in ('',' ','-','.') then Default_value='No_def_v';run;

 

What is wrong with it? I got in the new varibale only the first two digits, i.e. "No" without the rest "_def_v"

Tom
Super User Tom
Super User

Open a new topic if you have a new question.

Sir_Highbury
Quartz | Level 8
just done it

##- Please type your reply above this line. Simple formatting, no
attachments. -##

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 22 replies
  • 311132 views
  • 15 likes
  • 8 in conversation