'How to set the length of a created variable in SAS
I created a new variable (Racebin from Race) with an if statement in SAS. The length of the variable is 5 by default, so the categories of the new variable are truncated. How can I set its length to 20, for example?
I tried this:
data birth;
set WORK.birth;
if Race="White" then Racebin $20 = "White";
else Racebin $20 ="Not white";
run;
then this:
data birth;
set WORK.birth;
length Racebin $ 20;
if Race="White" then Racebin = "White";
else Racebin ="Not white";
run;
None of them works.
Solution 1:[1]
Your second code is the right way to define a NEW variable.
But since you seem to be overwriting your input dataset by using the same dataset name on both the DATA and SET statements then it is probable that the variable already exists. In that case the only way to change its length is to define it BEFORE the SET statement.
data birth;
length Racebin $20;
set birth;
if Race="White" then Racebin = "White";
else Racebin ="Not white";
run;
Or you could eliminate it from the input data by using the DROP= dataset option.
data birth;
set birth(drop=racebin);
length Racebin $20;
if Race="White" then Racebin = "White";
else Racebin ="Not white";
run;
You can avoid that issue of new variables already existing by not overwriting your inputs. Use a different name for the result of the DATA step than the input data it is reading.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
