I have data with over 50 variables coded as 0-1. How do I specifically change the length of those variables without changing the other variables?
data wide;
input enrolid x1$ x2 $ date1 date2 time;
datalines;1233 1 1 01/01/2012 . 111
4444 1 1 02/02/2012 . 33
666 0 1 . 02/03/2012 11
6666 0 1 03/03/2012 04/01/2012 11;
run;
I would say the real question is why are you treating 0/1 coded variables as character at all. A great many things can be done with 0/1 numeric values easily that cannot be done quite as simply with character values.
Sum of the variables gives a count of 1 values
Mean of the variables give a percent of 1 values
Range of the variables can tell if all the variables are the same
Lengths are set when created. You could use an ATTRIB, LENGTH or INFORMAT statement prior to the INPUT to read the variables as desired. For example, assuming that you want the X variables to be length 1.
data wide; length x1 - x2 $ 1; input enrolid x1$ x2 $ date1 date2 time; datalines;1233 1 1 01/01/2012 . 111 4444 1 1 02/02/2012 . 33 666 0 1 . 02/03/2012 11 6666 0 1 03/03/2012 04/01/2012 11; run;
You can not change the length of a variable once it is set. Why do you want to do this?
I would say the real question is why are you treating 0/1 coded variables as character at all. A great many things can be done with 0/1 numeric values easily that cannot be done quite as simply with character values.
Sum of the variables gives a count of 1 values
Mean of the variables give a percent of 1 values
Range of the variables can tell if all the variables are the same
Lengths are set when created. You could use an ATTRIB, LENGTH or INFORMAT statement prior to the INPUT to read the variables as desired. For example, assuming that you want the X variables to be length 1.
data wide; length x1 - x2 $ 1; input enrolid x1$ x2 $ date1 date2 time; datalines;1233 1 1 01/01/2012 . 111 4444 1 1 02/02/2012 . 33 666 0 1 . 02/03/2012 11 6666 0 1 03/03/2012 04/01/2012 11; run;
Thank you!
Note an INFORMAT statement is for attaching informats to the variables, not defining their length. If it is the first place where the variable is mentioned then it will have a side effect of forcing the compiler to guess what type/length to use for the variable. The same way that the compiler will guess if some other statement, like INPUT or an assignment statement, is the first place the variable appears.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.