Hi, here's my simple code that re-sets length of variable 'EmpID' after a SET statement.
data department;
input EmpID;
length EmpID 5;
datalines;
38284
47372
47372
93822
;
run;
data accounting;
set department;
length EmpID 6;
run;
Although original length of EmpID in department was 5, it changes to 6.
I was told that if re-declaration of length of a variable occurs after a set statement, it gets ignored,
since there is a preset length and data are already flowing in, line-by-line, by a SET statement.
Am I wrong? Well, at least from what I have found out in SAS 9.4, it seems I'm wrong
As the DATA step executes, all numerics have a length of 8. The length you assign using a LENGTH statement can truncate the value as it gets moved to an output data set.
For numerics, you can change the length of the numeric (once it gets output) in the way you describe.
For character variables, the first mention of the variable determines its length. To change the length, you would need to place the LENGTH statement before the SET statement. Placing it after should generate some sort of message (error or warning).
I was just using the fact that you can set the LENGTH for numeric variables after they have been defined in a little utility I was creating to generate data step code to post sample data.
If you run this program:
filename ds2post url 'https://raw.githubusercontent.com/sasutils/macros/master/ds2post.sas';
%inc ds2post;
%ds2post(sashelp.citiwk,obs=1);
It will generate this data step and show it in the log.
data work.citiwk (label='Citibase weekly indicators: DEC85-JAN92');
infile datalines dsd dlm='|' truncover;
input DATE MF3505 TCJ WSPCA WSPUA WSPIA WSPGLT HFBI20 FF142B FCPOIL ;
length DATE 7 ;
format DATE weekdatx16. ;
label DATE='Date of Observation'
MF3505='MONEY STOCK:M1(CURRENCY+DEMAND DEP+OTHER'
TCJ='INDUSTRIAL MATERIALS PRICE INDEX, 18 COM'
WSPCA='STANDARD & POOR''S WEEKLY BOND YIELD: COM'
WSPUA='STANDARD & POOR''S WEEKLY BOND YIELD: UTI'
WSPIA='STANDARD & POOR''S WEEKLY BOND YIELD:INDU'
WSPGLT='STANDARD & POOR;S WEEKLY BOND YIELD: GOV'
HFBI20='BOND BUYERS INDEX: 20 BOND GENERAL OBLIG'
FF142B='BOND YIELD:"A" UTILITY(RECENTLY OFFERED)'
FCPOIL='PETROLEUM, REFINED OIL PRICES: FUEL OIL,'
;
datalines4;
9487|||10.4339999966323|10.5379999987781|10.3289999999105|9.23999999836087|8.32999999821186|10.5899999998509|0.76749999984167
;;;;
So the INPUT statement will default the variable DATE to numeric. Then the LENGTH statement will tell SAS to truncate DATE to 7 bytes when it writes it to the dataset.
@jimmychoi wrote:
Hi, here's my simple code that re-sets length of variable 'EmpID' after a SET statement.
data department; input EmpID; length EmpID 5; datalines; 38284 47372 47372 93822 ; run; data accounting; set department; length EmpID 6; run;
Although original length of EmpID in department was 5, it changes to 6.
I was told that if re-declaration of length of a variable occurs after a set statement, it gets ignored,
since there is a preset length and data are already flowing in, line-by-line, by a SET statement.
Am I wrong? Well, at least from what I have found out in SAS 9.4, it seems I'm wrong
Why is EmpId numeric at all? Do you know that length of 5 does not mean, that only five digits can be stored in the variable? With a length of 6 the largest storable number is 237.
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.