BookmarkSubscribeRSS Feed
jimmychoi
Obsidian | Level 7

 

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

3 REPLIES 3
Astounding
PROC Star

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).

Tom
Super User Tom
Super User

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.

andreas_lds
Jade | Level 19

@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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2806 views
  • 5 likes
  • 4 in conversation