BookmarkSubscribeRSS Feed
kuridisanjeev
Quartz | Level 8

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

9 REPLIES 9
Keith
Obsidian | Level 7

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.

kuridisanjeev
Quartz | Level 8

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

BrunoMueller
SAS Super FREQ

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;
kuridisanjeev
Quartz | Level 8

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

BrunoMueller
SAS Super FREQ

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

JatinBansal
Calcite | Level 5

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;

pradeepalankar
Obsidian | Level 7

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;

Tom
Super User Tom
Super User

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

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

MikeZdeb
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 1436 views
  • 1 like
  • 7 in conversation