DATA Step, Macro, Functions and more

reading ; (semicolon) and , (comma) in the date and How to output in csv file

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 112
Accepted Solution

reading ; (semicolon) and , (comma) in the date and How to output in csv file

folks, could you please tell me which function to use to correct the name sothat either I get rid of ; and , 

or replace ; with , and use translate function in my output .csv file? 

   I am unable to read the data to begin with. 

         thanks in advance.  

 


data check;
input name $150.;
name_correct=compress(name,'','');
cards;
daniel - height 72 inches, weight 250 pounds;race african american
jack - height 60 inches, weight 150 pounds,race Caucasian
Lee - height 75 inches, weight 175 pounds,race mixed
;
run;
data _null_;
set check;
file "path.csv";
put 'demographics';
put 'name';
run;


Accepted Solutions
Solution
‎11-16-2017 06:47 AM
Super User
Super User
Posts: 8,127

Re: reading ; (semicolon) and , (comma) in the date and How to output in csv file

[ Edited ]

What are you trying to do? Are you trying to read that data into a dataset?  If so what do you want the result to look like?

It looks more like you are reading it in and then writing it back out again to another text file.  Why?

Perhaps you want to re-write it with the semi-colons replaced by commas?

data check;
  infile cards truncover ;
  input line $char150.; 
  line = translate(line,',',';');
  file 'path.csv' ;
  len = lengthn(line);
  put line $varying150. len ;
cards4;
daniel - height 72 inches, weight 250 pounds;race african american
jack - height 60 inches, weight 150 pounds,race Caucasian
Lee - height 75 inches, weight 175 pounds,race mixed
;;;;

 Perhaps you can just read it as it is?

data check;
  length name height weight race $100 ;
  infile cards dsd dlm='-,;' truncover ;
  input name height weight race ;
  if lowcase(height)=:'height' then height=left(substr(height,7));
  if lowcase(weight)=:'weight' then weight=left(substr(weight,7));
  if lowcase(race)=:'race' then race=left(substr(race,5));
cards4;
daniel - height 72 inches, weight 250 pounds;race african american
jack - height 60 inches, weight 150 pounds,race Caucasian
Lee - height 75 inches, weight 175 pounds,race mixed
;;;;

image.png

View solution in original post


All Replies
Frequent Contributor
Posts: 112

Re: reading ; (semicolon) and , (comma) in the date and How to output in csv file

sorry for the typo in the message heading (data instead of date)

Super User
Posts: 23,776

Re: reading ; (semicolon) and , (comma) in the date and How to output in csv file

They look like delimiters, why not treat them as such?

Frequent Contributor
Posts: 112

Re: reading ; (semicolon) and , (comma) in the date and How to output in csv file

Reeza,

            I want to get it out as it is or with commas or space delimiters, but as a single row. so we will have 3 rows in output. Getting semicolon out of my way is the big problem and I can't find solution. 

thanks,

Frequent Contributor
Posts: 112

Re: reading ; (semicolon) and , (comma) in the date and How to output in csv file

comma can be used as delimiter, but there is only one semicolon (Smiley Wink, how could we deal with it ??

Solution
‎11-16-2017 06:47 AM
Super User
Super User
Posts: 8,127

Re: reading ; (semicolon) and , (comma) in the date and How to output in csv file

[ Edited ]

What are you trying to do? Are you trying to read that data into a dataset?  If so what do you want the result to look like?

It looks more like you are reading it in and then writing it back out again to another text file.  Why?

Perhaps you want to re-write it with the semi-colons replaced by commas?

data check;
  infile cards truncover ;
  input line $char150.; 
  line = translate(line,',',';');
  file 'path.csv' ;
  len = lengthn(line);
  put line $varying150. len ;
cards4;
daniel - height 72 inches, weight 250 pounds;race african american
jack - height 60 inches, weight 150 pounds,race Caucasian
Lee - height 75 inches, weight 175 pounds,race mixed
;;;;

 Perhaps you can just read it as it is?

data check;
  length name height weight race $100 ;
  infile cards dsd dlm='-,;' truncover ;
  input name height weight race ;
  if lowcase(height)=:'height' then height=left(substr(height,7));
  if lowcase(weight)=:'weight' then weight=left(substr(weight,7));
  if lowcase(race)=:'race' then race=left(substr(race,5));
cards4;
daniel - height 72 inches, weight 250 pounds;race african american
jack - height 60 inches, weight 150 pounds,race Caucasian
Lee - height 75 inches, weight 175 pounds,race mixed
;;;;

image.png

Frequent Contributor
Posts: 112

Re: reading ; (semicolon) and , (comma) in the date and How to output in csv file

Tom,

         I have to clean this data and put it back as csv file. The first one works alright for me. Thanks,

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 188 views
  • 0 likes
  • 3 in conversation