DATA Step, Macro, Functions and more

How to read Input data

Reply
Super Contributor
Posts: 276

How to read Input data

Dear All,

I am back to to basics.

I am having a list of names in a text file in bellow manner.

"Sanjeev,Kuridi ;Ramesh ,varma;Rajesh,kumar" and so on.

Each name is combination  of first name and last with the delimter of " , " and every name is delimited with " ; ".

So i want to create a dataset with a variable name ,in that i have to put all the names.

I am not sure how to deal this kind of data.

I tried to copy all that text and kept under datalines,but getting error because of " ; ".

I wonder if any one have a solutions of this senario..

Thanks,

Sanjeev.K

Regular Contributor
Posts: 151

Re: How to read Input data

Check out DATALINES4, this works the same as DATALINES except it allows semi colons in the data.  You need to finish the code with 4 semi colons.

Super Contributor
Posts: 276

Re: How to read Input data

Hi Keith,

Thank you for your prompt.

Instead of copying the text from the file and pasting it in datalines ,is there any way to import the data directly ??

Thanks,

Sanjeev.K

SAS Super FREQ
Posts: 683

Re: How to read Input data

Hi

Have a look at the code below. the first DATA Step creates some data according to what you mentioned.

The second DATA Step reads the data from the text file using a semicolon as a delimiter for the firstname, lastname combination. The SCAN function then separates the two names.

filename sugus temp;

data _null_;
 
file sugus;
  put "Sanjeev,Kuridi ;Ramesh ,varma;Rajesh,kumar";
run;

data want;
  infile sugus dlm=";";
 
input
    fullName :
$256.
    @@
  ;
  length
    firstName $
64
    lastName $
64
  ;
  firstName = scan(fullName, 1, ",");
  lastName = scan(fullName, 2, ",");
run;

filename sugus clear;
Super Contributor
Posts: 276

Re: How to read Input data

Hi,

This works perfectly Bruno,Thanks for your code.

But is there any way to get the data directly with out doing copy paste of data into SAS.

Because if this is one time activity,your code is best suitable,but if it is daily activity,So need to automate this one instead of copying data into SAS everyday.

Any suggestions on this ???

Thanks again for your kind help.

Thanks,

Sanjeev.K

SAS Super FREQ
Posts: 683

Re: How to read Input data

where is the data coming from, a file, a FTP server, a URL, a...?

Occasional Contributor
Posts: 11

Re: How to read Input data

Hi

As you originally mentioned that data is in text file, the following code is working fine for me:

data this;

infile "D:\new.txt" dlm = ',;';

input (first last) (2* : $10.) @@;

run;

Frequent Contributor
Posts: 106

Re: How to read Input data

Hi Sanjeev, try this one , it will read the file directly:

data want(keep=first_name last_name);

infile 'infile.txt' truncover;

input line $1000.;  /*give here the max length for line*/

do i=1 to countc(line,';')+1;

first_name=scan(scan(line,i,';'),1,',');

last_name=scan(scan(line,i,';'),2,',');

output;

end;

run;

Super User
Super User
Posts: 6,500

Re: How to read Input data

To read directly from an external file use the INFILE statement.

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Valued Guide
Posts: 765

Re: How to read Input data

Hi ...

filename sugus temp;

data _null_;

  file sugus;

  put "Sanjeev,Kuridi ;Ramesh ,varma;Rajesh,kumar";

  put "zdeb, mike ; clinton, bill;";

  put "z, jay;";

run;

data new;

infile sugus dlm=',;';

input (last first) (: $20.) @@;

run;

proc print data=new noobs;

run;


output ...


last       first

Sanjeev    Kuridi

Ramesh     varma

Rajesh     kumar

zdeb       mike

clinton    bill

z          jay

Ask a Question
Discussion stats
  • 9 replies
  • 542 views
  • 1 like
  • 7 in conversation