BookmarkSubscribeRSS Feed
4 REPLIES 4
singhsahab
Lapis Lazuli | Level 10

Hello,

 

Welcome !! 

 

you have to add DSD option in infile statement to read correct data.

 

infile ‘c:\temp\PurchaseRecords.dat’ dlm=’,’ DSD;

SuryaKiran
Meteorite | Level 14

This is an interesting question and requires more than just basic knowledge to solve. If your just starting to lean SAS then these type might little confuse you.

 

"_INFILE_" to read the raw data line and then using do loop the extract the required fields.

 

data Purchase(DROP=i);
infile 'c:\temp\PurchaseRecords.dat';
input @;
	do i=1 to scan(_infile_,2,','); 
		id=scan(_infile_,1,',');
		unitpurchased=scan(_infile_,i+2,',','m');
		output;
	end;
run;

 - input @  holds the line

- scan(_infile_,2,',') - Extracting the second field ( C005,3,15,,39 ) from line will tell how many times the loop is required. 

- id=scan(_infile_,1,',') always the first record, extract first values from the line.

-unitpurchased= scan(_infile_,i+2,',','m') reading values from 3,4,5...... based on i value. Here fourth argument 'm' for scan() is required for consecutive blanks.

- output 

image.png

 

Thanks,
Suryakiran
Tom
Super User Tom
Super User

Use the DSD option to allow it to properly treat the adjacent delimiters as indicating a missing value. DSD implies DLM=','.  You should also add the TRUNCOVER option just in case the actual number of values is less than the number indicated.

You will need more variables to control the number of values to read.  You could drop them if you really don't need them.  But if you keep only two variables then you have nothing in the data that stores the original order of the values on the line.

data Purchase;
  infile 'c:\temp\PurchaseRecords.dat' dsd truncover;
  length id $8;
  input id number_of_visits @;
  do visit_no=1 to number_of_visits;
     input unitpurchased @;
     output;
  end;
  keep id visit_no unitpurchased;
run;

Also beware of Microsoft products converting your double quote and single quote characters into “stupid” quotes

Astounding
PROC Star

One more detail worth learning here.  Consider your original INPUT statement:

 

input id $8 visit_no @ unitpurchased @;

 

I would guess that your intent is to read 8 characters to get the value for ID.  But that's not what the program says.  It actually says to take the contents of column 8 (just a single character) as the value of ID.  If you want to give instructions that tell SAS to read 8 characters, you need to add a dot:

 

input id $8. visit_no @ unitpurchased @;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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