BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
eslb2022
Calcite | Level 5

I have a data set in a file that is separated with commas, missing data is simply left blank, and is made of characters and numbers. I need to read the data file into SAS and print the data set, but I believe the commas and missing data is not being read into SAS properly. I have attached a photo of the data set, the code I used, and the table SAS generated. How do I fix the code to read the data properly?

 

sas.PNG

 

Code:

DATA BenAndJerrys;
INFILE 'C:\Users\165\Downloads\BenAndJerrys.dat';
INPUT FlavorName $ PortionSize Calories CaloriesFromFat Fat SaturatedFat TransFat Cholesterol Sodium TotalCarbohydrates DietaryFiber Sugars Proteins YearIntroduced YearRetired ContentDescription $ Notes $ ;
PROC PRINT;
RUN;

 

sas2.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

Hello,

 

Can you try to put the DSD option on the INFILE statement?

The DSD option sets the comma as the default delimiter and if there are two adjacent commas it means two delimiters (a missing value is supposed to be in-between the two commas).

 

Koen

View solution in original post

6 REPLIES 6
Astounding
PROC Star

Where you have multiple commas in a row, and each marks a separate variable, you can tell SAS to expect that and accomodate it by adding dsd:

INFILE 'C:\Users\lgb156\Downloads\BenAndJerrys.dat' dsd;
eslb2022
Calcite | Level 5

That worked, but the flavor name and description columns will not fill with the entire context. The image shows that the Flavor Name for "Banana Split" only says "Banana: in the cell. How do I tell SAS to put the entire context in the cell?

sbxkoenk
SAS Super FREQ

Hello,

 

The default delimiter is a blank, so that's why it outputs "Banana" to your first variable and not "Banana Split".

With adding DSD option on the INFILE statement, you should also solve that, no??

If not we can work with an informat.

 

Koen

Tom
Super User Tom
Super User

You didn't define your variables before using them in the INPUT statement.  So SAS was forced the GUESS how to define them based on how you used them first.  Your input statement provides no other information it can use to guess other than the $ modifier so it will default all of the variables to use a length of 8. The ones with the $ after them it will default to character.  Any variable that did not have the $ modifier after it will default to numeric.

 

Add a LENGTH statement before the INPUT statement to set the length (and type) of the variables.  Once the type is known you don't need the $ modifiers in the INPUT statement anymore.

Tom
Super User Tom
Super User

You could also add informats in the INPUT to give SAS some additional information to base its guess of how to define the variables.  But make sure to use the : modifier so that the input statement does not read past or stop short of the delimiters.

 

DATA BenAndJerrys;
  INFILE 'C:\Users\lgb156\Downloads\BenAndJerrys.dat' dsd truncover;
  INPUT FlavorName :$30. PortionSize Calories CaloriesFromFat Fat SaturatedFat 
        TransFat Cholesterol Sodium TotalCarbohydrates DietaryFiber Sugars 
        Proteins YearIntroduced YearRetired 
        ContentDescription :$50. Notes :$200. 
   ;
run;
sbxkoenk
SAS Super FREQ

Hello,

 

Can you try to put the DSD option on the INFILE statement?

The DSD option sets the comma as the default delimiter and if there are two adjacent commas it means two delimiters (a missing value is supposed to be in-between the two commas).

 

Koen

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 6 replies
  • 1373 views
  • 2 likes
  • 4 in conversation