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

Hi,

 

my dataset is like:

GKL1 (char)
"10"
"13"
"40"
"16"

 

What I want is:

GKL2 (char)
"10"
"13"
"40_41"
"16"

 

So: Every "40" is to be transformed into "40_41". I tried it with the following Code, but it doesnt work. The problem seems to be the format/length of the new column. Do you have a solution? Thanks! Btw: Ive never understood whats the difference is between format/length and when I have to use which of both.

 

data Eigenges; 
set Eigenges; 
format GKL1 $12.;
gkl2=gkl1; 
if gkl1 in("40" "41") then GKL2="40_41";
run;

 

I also tried it with:

 

data Eigenges; 
set Eigenges; 
format GKL2 $12.;
gkl2=gkl1; 
if gkl1 in("40" "41") then GKL2="40_41";
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Programming 1: https://support.sas.com/edu/schedules.html?ctry=us&crs=PROG1

 

Please post the complete log.

data Eigenges; 
set Eigenges; 

I had not noticed, that you are overwriting your initial dataset all the time. This is a bad idea, because now you have to go back to square one and re-create "Eigenges" in its original form. Then please post proc contents of that file, especially the variable definition are required, so that we see what you really have.

View solution in original post

8 REPLIES 8
andreas_lds
Jade | Level 19

In non-unicode environments Length defines the number of chars you can store in a variable.

A format just changes the way a value is displayed.

To solve the problem add

length GKL $5;

after set statement.

Konkordanz
Pyrite | Level 9

Which one do I have to define? The "old" GKL1 or the new one "GKL2"? But regardless: The length-command doesnt change anything.

 

Edit: I got the following warning: Length of character variable gkl2 has already been set." Possibe hint for a solution?

 

 

data Eigenges; 
set Eigenges; 
length GKL1 $5.;
format GKL1 $12.;
gkl2=gkl1; 
if gkl1 in("40" "41") then GKL2="40_41";
run;

 

 

andreas_lds
Jade | Level 19

Sorry for the confusion, i meant GKL2.

You should take part in Programming 1, afaik it is free and you will learn all the basics one must know.

Konkordanz
Pyrite | Level 9

Programming 1? Whats that?

Back to my problem: It doesnt work neither with "GKL2". But I get the following warning: "Length of character variable gkl2 has already been set."

What does that mean? Thank you again for your help!

andreas_lds
Jade | Level 19

Programming 1: https://support.sas.com/edu/schedules.html?ctry=us&crs=PROG1

 

Please post the complete log.

data Eigenges; 
set Eigenges; 

I had not noticed, that you are overwriting your initial dataset all the time. This is a bad idea, because now you have to go back to square one and re-create "Eigenges" in its original form. Then please post proc contents of that file, especially the variable definition are required, so that we see what you really have.

Konkordanz
Pyrite | Level 9

Ah okay, thats an important information for me. It works with a new dataset. So that means: I cant change the length/format, if I refer to the same dataset, right?

Astounding
PROC Star

Since you have made many attempts to get the result you want, each time replacing Eigenges, your data set already contains GKL2.  So your task is really to replace the variable, not to create it.

 

data Eigenges; 
length GKL2 $ 5;
set Eigenges; 
gkl2=gkl1; 
if gkl1 in("40" "41") then GKL2="40_41";
run;

To replace GKL2:

  • The format statement will not do it.  You need a length statement.
  • The length statement must come before the set statement.

(Technically the FORMAT statement could work if you place it before the SET statement.)

Konkordanz
Pyrite | Level 9

Great, thank you!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 8 replies
  • 1084 views
  • 2 likes
  • 3 in conversation