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;
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.
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.
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;
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.
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!
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.
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?
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:
(Technically the FORMAT statement could work if you place it before the SET statement.)
Great, thank you!
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.
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.
Ready to level-up your skills? Choose your own adventure.