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
is this name Martines Terasa separated by blank making the name values separated by blank or comma?
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
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
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;
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;
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;
@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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.