Look at your data. Every field is in certain columns. I'll leave the rest for you to figure out.
What is the syntax that I am supposed to use if I want to "call" the columns that I need. I know I would have to probably make a zip and areacode variable but how can I make it so that it's like zip = townstatezip's column 31-36?
That is called column mode input. Here is the link to the Help page for it.
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000148066.htm
You should read the other pages on the other ways that the INPUT statement can be used.
Not exactly what I was asking. I was asking is it possible to only keep my original 6 variables but somehow create a new variable(without an input statement) that would only use parts of the previously created variable?
I have this so far but there's parts of it that needs to be changed because it doesn't work.
data child;
infile 'E:\School Stuff\ST445\child4.txt' pad; *Reading the file in and padding the end with spaces so it can tell when a new record begins*/;
input centerid; *Inputting every variable in one by one because they are given vertically*/;
input name $ 1-36;
input address $ 1-36;
input townstatezip $ 1-36 ;
input aphone $ 1-36 ;
input class $ 1-36;
stars = .;
if (class = 'One') then stars = 1;
else if (class = 'Two') then stars = 2;
else if (class = 'Three') then stars = 3;
else if (class = 'Four') then stars = 4;
else if (class = 'Five') then stars = 5;
if (areacode != 828 or 704 or 980 or 336 or 919 or 910 or 252) then put 'Error';
run;
In my assignment I have to change the "Class" to a variable and one, two, three, four, five obviously have different lengths for different records. I understand that if I read that line as 2 variables I could just use free format but like I said I was wondering if I could create a new variable without having to use "input."
Leon,
Let's be frank! You are asking the forum to do your homework for you. In the code you provided you are trying to create a statement based on area code, but you haven't yet created that variable.
Also, you try to check for parts of a string (i.e., class), but don't have any strings that could possibly match your condition.
I would suggest that you first really do try to solve the problem on your own and, when you run into problems, ask the forum for advice.
We cannot do your homework for you.
I understand that you cannot do my homework for me, but you should notice that my questsions aren't asking for the exact code for everything. I was just asking if there was a way to create a variable that uses only parts of an old variable. I have also taken the links that people gave me to figure more stuff out by myself. I had said that my code was incorrect, that part will work once I get the first parts correct. I also mentioned that through my past few homework I looked at examples of code from class and basically "copied" parts of it. However, for this homework it is much more difficult because the stuff we did in class is not exactly like this. I have currently changed my code to
data child;
infile 'E:\School Stuff\ST445\child4.txt' pad; *Reading the file in and padding the end with spaces so it can tell when a new record begins*/;
input centerid; *Inputting every variable in one by one because they are given vertically*/;
input name $ 1-36;
input address $ 1-36;
input townstate $ 1-30 zip 31-35 ;
input areacode 2-4 phone $ 6-36 ;
input class $ License $1-36;
stars = 0;
if (class = 'One') then stars = 1;
else if (class = 'Two') then stars = 2;
else if (class = 'Three') then stars = 3;
else if (class = 'Four') then stars = 4;
else if (class = 'Five') then stars = 5;
stars = '.';
if (areacode ne 828 or 704 or 980 or 336 or 919 or 910 or 252) then put 'Error'; *(I will later make a variable that will count the errors)
run;
This would do everything I need it to do, but like I said I'm not sure if we are supposed to do it like this because the teacher said " Each center takes six records in the file." but in this case I'd have eight. I was just wondering if once again from the original question if I could input 6 things and use only certain parts of the input to create other variables. If this is not possible then I will leave my code as above. I will try to continue to work on this in this way and possibly ask other questions if I come to a halt.
OK, I'm pressed that you actually answered part of your question on your own. However, when a number of the forum members have suggested code, you have said that you can't use it because you haven't learned it yet.
Your current problem is to identify whether something exists in a string. Have you learned about the index function yet or any other functions that can identify parts of a string or whether certain things/substrings exist in a given string?
No I'm pretty sure we have not learned much about strings or an indexing function. I will just continue doing the code as it is. Still working on it atm, noticed my counter was saying everything was wrong so that means there must be something wrong with my if statement logic, I'll try to figure it out myself though. I'm thinking I can't use
if (areacode ne 828 or 704 or 980 or 336 or 919 or 910 or 252) then Errorcount + 1;
put Errorcount;
I could be getting SAS code confused with other code(I've taken JAVA before). I'm guessing using "or" isn't correct. I guess I should put in multiple if, else if statements. You don't need to answer this, because I'm about to try it out.
Then think about it. Aren't the characters you are trying to identify all within the first three characters of the line you are reading in for class? i.e., One Two Thr Fou Fiv
Look at the 'in' operator
He can only use things which have already been covered in his class which is why I'm trying to get him to suggest the possible alternatives. Gee, now I'm starting to sound like an educator. Oh, wait, I used to be one of them!
Pretty much the same as others provided:
data foo;
infile cards truncover dlm=', ()';
input #1 id
#2 company_name $36.
#3 street $36.
#4 city & :$26. state :$10. zip :$5.
#5 arecode :$3. phone :$36.
#6 policy $36.;
cards;
68000482
CHILDREN'S CAMPUS OF CHAPEL HILL II
1620 MLK JR BLVD
CHAPEL HILL, NC 27514
(919) 967-5020
Temporary License
92002687
"HAPPY FACES" CHILD CARE CENTER
4500 EMMIT DRIVE
RALEIGH, NC 27604
(919) 231-0783
One Star Family CC Home License
60002518
'A' IS FOR APPLE
11919 PLANTERS ESTATES DRIVE
CHARLOTTE, NC 28278
(704) 504-8977
Five Star Center License
32001484
'A' PRECIOUS ANGEL DAYCARE
916 SOUTH MINERAL SPRINGS ROAD
DURHAM, NC 27703
(919) 957-8496
Three Star Family CC Home License
64000406
'AGAPE' CENTER OF LOVE'
203 N WALNUT STREET
SPRING HOPE, NC 27882
(252) 885-6249
GS 110-106
;
run;
Obs | id | company_name | street | city | state | zip | arecode | phone | policy |
---|---|---|---|---|---|---|---|---|---|
1 | 68000482 | CHILDREN'S CAMPUS OF CHAPEL HILL II | 1620 MLK JR BLVD | CHAPEL HILL | NC | 27514 | 919 | 967-5020 | Temporary License |
2 | 92002687 | "HAPPY FACES" CHILD CARE CENTER | 4500 EMMIT DRIVE | RALEIGH | NC | 27604 | 919 | 231-0783 | One Star Family CC Home License |
3 | 60002518 | 'A' IS FOR APPLE | 11919 PLANTERS ESTATES DRIVE | CHARLOTTE | NC | 28278 | 704 | 504-8977 | Five Star Center License |
4 | 32001484 | 'A' PRECIOUS ANGEL DAYCARE | 916 SOUTH MINERAL SPRINGS ROAD | DURHAM | NC | 27703 | 919 | 957-8496 | Three Star Family CC Home License |
5 | 64000406 | 'AGAPE' CENTER OF LOVE' | 203 N WALNUT STREET | SPRING HOPE | NC | 27882 | 252 | 885-6249 | GS 110-106 |
Message was edited by: Matthew Kastin I noticed you wanted to separate area code and phone number too.
Ugh now I'm stuck trying to loop this, I don't see why my logic is wrong...
Errorcount = 0;
if areacode ne 828 or areacode ne 704 or areacode ne 980 or areacode ne 336 or areacode ne 919 or areacode ne 910 or areacode ne 252 then Errorcount + 1;
else;
put Errorcount;
This way will show that my logic is wrong but I saw from some website you can use the 'or' statement. I tried other ways but the counter is always wrong. I could make 7 different if statements, but it would still have the incorrect counter. It would tell me if one area code doesn't satisfy the conditions though if I made it put 'Problem/Error'(although we would see 7 of them if all of them show up as 'problem/Error' then it is a real error).
From the SAS documentation page on if, then, else statements they have an example; they use the or statement, but it's not working for me...
data region;
input city $ 1-30;
if city='New York City'
or city='Miami' then
region='ATLANTIC COAST';
else if city='San Francisco'
or city='Los Angeles' then
region='PACIFIC COAST';
datalines;
...more data lines...
How did you create the AREACODE variable? Is it numeric or character? What type of error message does SAS give you?
What type of "loop" are you trying to run? Remember that SAS already is running a implicit loop over all of the observations in the dataset.
I made the areacode numeric; I'm not getting an error message, It's just when I use "Put Errorcount"; I can see that the counter is wrong. I can put all the code I have here atm if it would help, I just got the stars part correct(change all character vars into a numeric). Trying to work through the logic that if none of the area codes = those 7 different codes then it should note that it is an "invalid area code". After I do that correctly my next step would be to make SAS consider 704 and 980 as the "same" (i need to do a proc freq on it later and have it count these 2 as the same, that's what i commented out atm for later testing).
data child;
infile 'E:\School Stuff\ST445\child4.txt' pad; *Reading the file in and padding the end with spaces so it can tell when a new record begins*/;
input centerid; *Inputting every variable in one by one because they are given vertically*/;
input name $ 1-36;
input address $ 1-36;
input townstate $ 1-30 zip 31-35 ;
input areacode 2-4 phone $ 6-36 ; put areacode;
input class $ License $1-36;
stars = 0;
if (class = 'One') then stars = 1;
else if (class = 'Two') then stars = 2;
else if (class = 'Three') then stars = 3;
else if (class = 'Four') then stars = 4;
else if (class = 'Five') then stars = 5;
else stars = '.';
put stars;
Errorcount = 0;
if (areacode ne 828) or (areacode ne 704) or (areacode ne 980) or (areacode ne 336) or (areacode ne 919) or (areacode ne 910) or (areacode ne 252) then Errorcount + 1;
else;
put Errorcount;
/* if areacode = 980 or areacode = 704 then Charlotte $ = areacode; */
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: