BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I saw this code of creating a data set and I want to ask 2 questions pease:

1-In the input statement  for city var it is defined  : $12.

What is the purpose of the double points ?

If it is written as $12. instead of : $12. will it be same?

As I see the purpose is to define length of 12 for var city

2-What is the purpose of using Attriv in this example?

Can't we just write the following statement?

label revenue = ‘Parking Revenue’ date ='Business Date';

 

data tbl;
 attrib date label = 'Business Date';
 informat revenue  dollar12.2;
 format revenue dollar12.2; 
label revenue = ‘Parking Revenue’;
 input date city : $12.   revenue ; 
cards;
 20050117 Edmonton $145,234.72
 20050204 Edmonton $221,691.00
 20050328 Edmonton $375,876.54
 20050413 Edmonton $597,112.23
 20050510 Edmonton $726,432.02
;
 run;
3 REPLIES 3
ballardw
Super User

@Ronein wrote:

Hello

I saw this code of creating a data set and I want to ask 2 questions pease:

1-In the input statement  for city var it is defined  : $12.

What is the purpose of the double points ?

If it is written as $12. instead of : $12. will it be same?

As I see the purpose is to define length of 12 for var city

2-What is the purpose of using Attriv in this example?

Can't we just write the following statement?

label revenue = ‘Parking Revenue’ date ='Business Date';

 

data tbl;
 attrib date label = 'Business Date';
 informat revenue  dollar12.2;
 format revenue dollar12.2; 
label revenue = ‘Parking Revenue’;
 input date city : $12.   revenue ; 
cards;
 20050117 Edmonton $145,234.72
 20050204 Edmonton $221,691.00
 20050328 Edmonton $375,876.54
 20050413 Edmonton $597,112.23
 20050510 Edmonton $726,432.02
;
 run;

I would suggest running the code without the : and examine the results. It may also be more educational if you use a couple of other values instead of Edmonton such as "ABC" and "EdmontonABCD"  on a couple of records so the actual value of City changes length.

 

I have a slightly snarky question: When SAS was installed on your system were the online help links not included?

Tom
Super User Tom
Super User

That's a real mish-mash of coding styles.

The LABEL= option of the ATTRIB statement does the same thing as the LABEL statement.  Just like the LENGTH= , FORMAT= , and INFORMAT= options do the same things as the respectively named statements.  One advantage of the ATTRIB statement is you can specifiy all of the settings for a single variable in a single place.  Conversely the advantage of the original statements is that you can specify that attribute for many different variables in a single place.

 

The purpose of the COLON modifier in the INPUT statement it to prevent SAS from reading the variable in FORMATTED mode instead of LIST mode.  If you leave it off then SAS will always read exactly 12 bytes for CITY.  And if the value is shorter then 12 characters it might read the beginning of the text that is supposed to be the value of REVENUE .  Why would someone add the $12. informat to the input statement?  SAS does not need be told to use a special informat to read character strings, the default informat works fine (unlike for the value or REVENUE where the default numeric informat wouldn't understand the $ and , in the line).   But since this is the first place you referenced the variable CITY SAS will use the type and length of the informat to decide to make CITY as character with a length of 12 bytes.  Just like it decided that you meant to make REVENUE as a numeric variable because the first place it appears is in the INFORMAT statement and you are assigning a numeric informat to the variable.

Why are you assigning an informat to REVENUE that will divide input strings that don't have an explicit decimal point by 100? Unless you know that the text being read specifically was created without the decimal point you should never include a decimal value on and informat. Also note that the DOLLAR informat is just an alias for the COMMA informat. 

Also why are you giving a variable named DATE values like 20,050,117  instead of setting them to actual date values? That will make them hard to deal with.

Also why did you indent the lines of data instead of starting them in column one?

Also why did you include the extra RUN statement after the end of the data step?

data tbl;
  attrib date length=8 format=yymmdd10. informat=yymmdd. label='Business Date';
  attrib city length=$12 ;
  attrib revenue length=8 format=dollar12.2 informat=comma. label='Parking Revenue';
 input date city revenue ; 
cards;
20050117 Edmonton $145,234.72
20050204 Edmonton $221,691.00
20050328 Edmonton $375,876.54
20050413 Edmonton $597,112.23
20050510 Edmonton $726,432.02
;
Obs          date      city           revenue

 1     2005-01-17    Edmonton     $145,234.72
 2     2005-02-04    Edmonton     $221,691.00
 3     2005-03-28    Edmonton     $375,876.54
 4     2005-04-13    Edmonton     $597,112.23
 5     2005-05-10    Edmonton     $726,432.02

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1920 views
  • 2 likes
  • 4 in conversation