DATA Step, Macro, Functions and more

How i read vishal.csv file into SAS ?

Reply
Frequent Learner
Posts: 1

How i read vishal.csv file into SAS ?

[ Edited ]

Hi,

This is my vishal.csv file. How will i read this file in SAS  ? I tried to read like below-

data abc;

infile "vishal.csv" dsd ;

input name$ month$ score1 score2 score3 score4 score5 situatuon$ ;

run;

vishal.csv--

JOHN,MAR,11,22,33,44,FINAL
JOHN,FEB,55,66,77,88,99,CURRENT
TINA,MAR,12,13,14,15,FINAL
TINA,FEB,17,18,19,20,21,CURRENT

Super User
Posts: 19,822

Re: How i read vishal.csv file into SAS

Posted in reply to Vishal1234

Is there a question here?

 

Please review the guidelines on how to post a question.

https://communities.sas.com/t5/SAS-Communities-Library/How-to-ask-a-question-in-SAS-Analytics-U-Comm...

 

There are video's on the most common tasks here:

support.sas.com/training/tutorial/

Super User
Posts: 19,822

Re: How i read vishal.csv file into SAS ?

Posted in reply to Vishal1234

1. Make sure the path is correct, you usually need the full path

2. You have 4 scores not 5, so trying to read a score5 will mess things up. Remove it from your INPUT list.

Super User
Posts: 11,343

Re: How i read vishal.csv file into SAS ?

Posted in reply to Vishal1234

If you did not copy the lines shown directly from the file then what you show will have issues as some records have 4 scores and others have 5.

If the data is well formed csv the data would look like:

JOHN,MAR,11,22,33,44,,FINAL
JOHN,FEB,55,66,77,88,99,CURRENT
TINA,MAR,12,13,14,15,,FINAL
TINA,FEB,17,18,19,20,21,CURRENT

 

Note the inserted extra comma in lines 1 and 3 where there are only 4 scores to idicate Score5 is missing,

or the extra comma might appear between any score variable and adjacent depending on which score is actually missing.

 

If you do not have well formed CSV and the actual number of scores varies you will have to do a fair bit of work especially if the number of missing scores is more than 1. It appears that there may be 5 scores only when the situation is "CURRENT" and 4 scores when situation is "FINAL". If that is the case you will have to provide some code to indicate which which line is which.

 

Something like this:

data abc;
   infile datalines dlm=',' dsd ;
   input @;
   if findw(_infile_,'CURRENT')>0 then
         input name$ month$ score1 score2 score3 score4 score5 situatuon$ ;
   else  if findw(_infile_,'FINAL')>0 then
         input name$ month$ score1 score2 score3 score4  situatuon$ ;   
   else input;
datalines;
JOHN,MAR,11,22,33,44,FINAL
JOHN,FEB,55,66,77,88,99,CURRENT
TINA,MAR,12,13,14,15,FINAL
TINA,FEB,17,18,19,20,21,CURRENT
;
run;
Ask a Question
Discussion stats
  • 3 replies
  • 139 views
  • 4 likes
  • 3 in conversation