BookmarkSubscribeRSS Feed
Best
Calcite | Level 5

data springgrads;
infile cards dsd dlm= ' '
input students $15 concentration $15 GPA;
cards;
Johnson,Allison Biochemistry 2.56
Davis,jeffrey General Biology 3.91
Hall,Baniel Genetics 3.43
Hill,Courtney Ecology 4.00
Scott,Tiffany plan Biology 2.56
Martines Terasa zoology 3.21
;
run;

 

please let me how i can read the above data. thanks in advance

9 REPLIES 9
novinosrin
Tourmaline | Level 20

is this name Martines Terasa separated by blank  making the  name values separated by blank or comma?

Best
Calcite | Level 5
Sorry I missed to mention comma between them

Martinez,Terasa
novinosrin
Tourmaline | Level 20

You probably will  need call scan to the get the position of the 3rd variable and $varying informat to read the value of 2nd  by calculating the length using the position

 

How i wish I was sitting with sas software to demo 😞  so sorry

 

2nd option : you could use _infile_ var and scan the _infile_ with a loop.

 

3rd: APP  functions instead of scan mentioned in  2nd 

Best
Calcite | Level 5
Is there any other method, please write the code?
novinosrin
Tourmaline | Level 20

i'd love to but I don't have sas software at home. I'm sure somebody will give you the code , if not I'll tomorrow morning 

andreas_lds
Jade | Level 19

Have you tried what @novinosrin suggested? What problems you ran into?

 

Here is a solution without call scan using char-functions to extract the data from the input-buffer. I am almost sure that there is a solution using only input-statement and some funky options.

data springgrads;
   length 
      student $ 30 
      concentration $ 15 
      GPA 8
      c_start c_length 8
   ;

   drop c_start c_length;

   format GPA 5.2;

   infile cards;
   input;

   Student = scan(_infile_, 1, ' ');
   c_start = findc(_infile_, ' ') + 1;
   c_length = anydigit(_infile_) - c_start;
   Concentration = substr(_infile_, c_start, c_length);
   GPA = input(scan(_infile_, countw(_infile_, ', '), ', '), best32.);  

   cards;
Johnson,Allison Biochemistry 2.56
Davis,jeffrey General Biology 3.91
Hall,Baniel Genetics 3.43
Hill,Courtney Ecology 4.00
Scott,Tiffany plan Biology 2.56
Martines,Terasa zoology 3.21
;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please make sure your code works before posting.  With a few changes:

data springgrads;
  infile cards dsd dlm=' ';
  length students concentration $15;
  input students $ concentration $ GPA;
datalines;
Johnson,Allison Biochemistry 2.56
Davis,jeffrey General Biology 3.91
Hall,Baniel Genetics 3.43
Hill,Courtney Ecology 4.00
Scott,Tiffany plan Biology 2.56
Martines Terasa zoology 3.21
;
run;

It now runs.  However it will not give you the required output and the reason is simple.  Data needs to a definitive and unique delimiter character value.  In your given data you do not have this.  Therefore it comes down to you to code in the logic required to pull out each data item, there is not automated method to guess based on a garbage data source.  If it was me I would be sending the data back with instructions on how to create a well formed data source.  So to get your data to work correctly:

data springgrads;
  infile cards dsd dlm=' ';
  length students concentration $15;
  input students $ concentration $ GPA;
datalines;
"Johnson,Allison" "Biochemistry" 2.56
"Davis,jeffrey" "General Biology" 3.91
"Hall,Baniel" "Genetics" 3.43
"Hill,Courtney" "Ecology" 4.00
"Scott,Tiffany" "plan Biology" 2.56
"Martines Terasa" "zoology" 3.21
;
run;
novinosrin
Tourmaline | Level 20

Good morning @Best and others in the thread, I thought you would have tried something and come back to us for further clarification/questions. Anyways here you go:

 

data springgrads;
infile cards truncover col=_c ;
input students : $15. @;
call scan(_infile_, -1, _p, _length,' ');
_l=_p-1-_c;
input concentration $varying15. _l GPA;
drop _:;
cards;
Johnson,Allison Biochemistry 2.56
Davis,jeffrey General Biology 3.91
Hall,Baniel Genetics 3.43
Hill,Courtney Ecology 4.00
Scott,Tiffany plan Biology 2.56
Martines,Terasa zoology 3.21
;
run; 

novinosrin
Tourmaline | Level 20

@Best Another one although I like the previous one(personal preference) 

 

data springgrads;
infile cards truncover ;
informat students $15. concentration $15.;
input  @;
GPA=input(scan(_infile_,-1,' '),8.);
_infile_=substr(_infile_,1 ,anydigit(_infile_)-1);
input students  concentration & ;
cards;
Johnson,Allison Biochemistry 2.56
Davis,jeffrey General Biology 3.91
Hall,Baniel Genetics 3.43
Hill,Courtney Ecology 4.00
Scott,Tiffany plan Biology 2.56
Martines,Terasa zoology 3.21
;
run; 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1064 views
  • 0 likes
  • 4 in conversation