BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
bhca60
Quartz | Level 8

I am trying to replace blanks in a column by creating a new column - "ent1" but the value "The Hav" is not carrying over as "The Hav",
it's carrying over as "The ". I tried doing length, format and informat but it still shows as "The " in the output. When checking the
properties for the new field "ent1", it still shows length as 3 and format and informat as $10.

data getb;
set geta;
length ent1 $10.; format ent1 $10.; informat ent1 $10.;
if bus="rnm" and ent ne 'lfs' then ent1='soc'; if bus='snc' and ent = '' then ent1='snc'; else ent1=ent; if ent1 = 'The ' then ent1 = 'The Hav'; run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The only reason that ENT1 would not have a length of $10 in GETB is if ENT1 already existed in GETB.

 

Try dropping it on the way in.  There is no need to include a period in the LENGTH statement, lengths are always integers. There is no need to attach either an informat nor a format to normal character variables.  SAS does not need special instructions for display them.  And since you are not actually reading in data there is no need for an informat at all.

 

Your second IF statement (the one with the ELSE statement associated) will override any choices made by the first.  Perhaps you need another ELSE?

data getb;
  set geta(drop=ent1);
  length ent1 $10;
  if bus="rnm" and ent ne 'lfs' then ent1='soc';
  else if bus='snc' and ent = '' then ent1='snc';
  else ent1=ent;

  if ent1 = 'The ' then ent1 = 'The Hav';
run;

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

The only reason that ENT1 would not have a length of $10 in GETB is if ENT1 already existed in GETB.

 

Try dropping it on the way in.  There is no need to include a period in the LENGTH statement, lengths are always integers. There is no need to attach either an informat nor a format to normal character variables.  SAS does not need special instructions for display them.  And since you are not actually reading in data there is no need for an informat at all.

 

Your second IF statement (the one with the ELSE statement associated) will override any choices made by the first.  Perhaps you need another ELSE?

data getb;
  set geta(drop=ent1);
  length ent1 $10;
  if bus="rnm" and ent ne 'lfs' then ent1='soc';
  else if bus='snc' and ent = '' then ent1='snc';
  else ent1=ent;

  if ent1 = 'The ' then ent1 = 'The Hav';
run;
bhca60
Quartz | Level 8

Thank you! This worked for me without the else or drop (ent1 is the new column; i assume you meant that I should drop ent instead which is the orig column):

data getb ;
set geta;
length ent1 $10;

if bus="rnm" and ent ne 'lfs' then ent1='soc';

if bus='snc' and ent = '' then ent1='snc';
else ent1=ent;

if ent1 = 'The ' then ent1 = 'The Hav';
run;
Tom
Super User Tom
Super User

That is the same program you started with.

Perhaps it works now because GETA is different now than it was before?

 

The logic of your code is still broken.  Consider an observation with (bus="rnm" and ent ne 'lfs') then the first IF/THEN will set ENT1 to 'soc'.  But then the next IF/THEN/ELSE block will set it to whatever value the ENT had before.  Which value do you want in that case?

 

Note: You do NOT want to drop ENT on the way in because your code is using the values of ENT in its logic.  If GETA might or might not have the ENT1 variable you could adjust the setting of the DKRICOND system option.

 

Syntax

DKRICOND= ERROR | WARN | WARNING | NOWARN | NOWARNING
bhca60
Quartz | Level 8

This is what the data looked like before (blanks in rnm and snc) then I wanted to bring in the rest of the values from ent into ent1 column and use ent1 the rest of the program:

bus    ent   

rnm    lfs

rnm    lfs

rnm

rnm

snc    snc

snc

snc

ine    The Havens

ine    The Havens

 

bhca60
Quartz | Level 8

Actually, now i see what you're saying. Thank you it worked!

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 791 views
  • 0 likes
  • 2 in conversation