BookmarkSubscribeRSS Feed
harveysarah0
Calcite | Level 5

Using an organized version of the Mouse Weights data set from HW 2, complete the

following.

a) Import the organized data set to SAS. Calculate the difference between the Day 21

weight and the Day 3 weight for each mouse. Present it as a new column in SAS and

print only the mouse number and the weight difference.

14 REPLIES 14
Krueger
Pyrite | Level 9

What exactly is your question?

harveysarah0
Calcite | Level 5

I need help working through this question

harveysarah0
Calcite | Level 5

How do I input this data set into SAS?

Krueger
Pyrite | Level 9

1. Import the data you want. https://documentation.sas.com/?docsetId=proc&docsetTarget=n18jyszn33umngn14czw2qfw7thc.htm&docsetVer...

 

2. Add new column with your calculation. (There are many examples of how to do this on google) https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-calculated-column-in-an-existing-da...

 

If you run into errors with the above ask away.

 

Edit: As I feel it's important to mention, google is your friend with learning anything. When it comes to programming you can generally either find an example of someone doing what you are attempting or at the very least find documentation. The sooner you get used to this the easier it's going to be.

harveysarah0
Calcite | Level 5

This is the code I used. 

 

Proc Import data mice;
input 
cards;
;
Mouse Day3 Day6 Day9
Day12 Day15 Day18 Day21
1 0.109 0.388 0.621
0.823 1.078 1.132 1.191
2 0.218 0.393 0.568
0.729 0.839 0.852 1.004
3 0.394 0.549
0.700 0.783 0.870 0.925
4 0.209 0.419 0.645
0.850 1.001 1.026 1.069
5 0.193 0.362 0.520
0.530 0.641 0.640 0.751
6 0.201 0.361 0.502
0.530 0.641 0.640 0.751
7 0.202 0.370 0.498
0.650 0.795 0.858 0.910
80.1900.350
0.666 0.819 0.879 0.929
9 0.219 0.399 0.578
0.699 0.709 0.822 0.953
10 0.255 0.400 0.545
0.690 0.796 0.825 0.836
11 0.224 0.381 0.577
0.756 0.869 0.929 0.999
12 0.187 0.329 0.441
0.525 0.621 0.796
13 0.278 0.471 0.606
0.770 0.888 1.001 1.105
 
This is my output.

 

NOTE: The SAS System stopped processing this step because of errors.
1 Proc Import data mice;
----
22
202
ERROR 22-322: Syntax error, expecting one of the following: ;, DATAFILE, DATATABLE, DBMS, DEBUG,
FILE, OUT, REPLACE, TABLE, _DEBUG_.

ERROR 202-322: The option or parameter is not recognized and will be ignored.

2 input
3 cards;
4 ;
5 Mouse Day3 Day6 Day9
6 Day12 Day15 Day18 Day21
7 1 0.109 0.388 0.621
8 0.823 1.078 1.132 1.191
9 2 0.218 0.393 0.568
10 0.729 0.839 0.852 1.004
11 3 0.394 0.549
12 0.700 0.783 0.870 0.925
13 4 0.209 0.419 0.645
14 0.850 1.001 1.026 1.069
15 5 0.193 0.362 0.520
16 0.530 0.641 0.640 0.751
17 6 0.201 0.361 0.502
18 0.530 0.641 0.640 0.751
19 7 0.202 0.370 0.498
20 0.650 0.795 0.858 0.910
21 8 0.190 0.350
22 0.666 0.819 0.879 0.929
23 9 0.219 0.399 0.578
24 0.699 0.709 0.822 0.953
25 10 0.255 0.400 0.545
26 0.690 0.796 0.825 0.836
27 11 0.224 0.381 0.577
28 0.756 0.869 0.929 0.999
29 12 0.187 0.329 0.441
30 0.525 0.621 0.796
31 13 0.278 0.471 0.606
32 0.770 0.888 1.001 1.105

 
Tom
Super User Tom
Super User

In SAS there two types of steps. DATA steps and PROC steps.  You seem to have mixed the syntax of the two into one step.

 

Based on the text in your original attachment and the copy you pasted into this code as in-line data lines you do NOT want to try to run PROC IMPORT on this data. It does not know how to read data that spans lines.

 

So try writing a data step to read the file.  Hint: You do not want to read only one variable, and you don't want to name that variable CARDS.

harveysarah0
Calcite | Level 5

Like this? 

 

data mice;
input 
;
Mouse Day3 Day6 Day9
Day12 Day15 Day18 Day21
1 0.109 0.388 0.621
0.823 1.078 1.132 1.191
2 0.218 0.393 0.568
0.729 0.839 0.852 1.004
3 0.394 0.549
0.700 0.783 0.870 0.925
4 0.209 0.419 0.645
0.850 1.001 1.026 1.069
5 0.193 0.362 0.520
0.530 0.641 0.640 0.751
6 0.201 0.361 0.502
0.530 0.641 0.640 0.751
7 0.202 0.370 0.498
0.650 0.795 0.858 0.910
80.1900.350
0.666 0.819 0.879 0.929
9 0.219 0.399 0.578
0.699 0.709 0.822 0.953
10 0.255 0.400 0.545
0.690 0.796 0.825 0.836
11 0.224 0.381 0.577
0.756 0.869 0.929 0.999
12 0.187 0.329 0.441
0.525 0.621 0.796
13 0.278 0.471 0.606
0.770 0.888 1.001 1.105
Krueger
Pyrite | Level 9

So the article I sent previously provides examples (https://documentation.sas.com/?docsetId=proc&docsetTarget=n1w1xy48wd290bn1mqalitn7wy13.htm&docsetVer...) on how to do this. Your data in the text file needed to be reformatted (Just line everything up so all the variables are separated with your TAB button and all columns are on top) correctly but once done you can use the following:

 


proc import datafile='C:\MiceWeight.txt' /* Your path goes here directly to the .txt file */
            out=have
            dbms=dlm
            replace;
			delimiter='09'x;
run;

data want;
	set have;
	weightdiff = day21-day3;
run;

 

I'd recommend you try following through with @Tom as well and his solution which is a different approach from this but will probably make more sense to you once you actually learn it. Best of luck!

harveysarah0
Calcite | Level 5

PROC IMPORT data;
Mouse Day3 Day6 Day9 Day12 Day15 Day18 Day21
1 0.109 0.388 0.621 0.823 1.078 1.132 1.191
2 0.218 0.393 0.568 0.729 0.839 0.852 1.004
3 0.394 0.549 0.700 0.783 0.870 0.925
4 0.209 0.419 0.645 0.850 1.001 1.026 1.069
5 0.193 0.362 0.520 0.530 0.641 0.640 0.751
6 0.201 0.361 0.502 0.530 0.641 0.640 0.751
7 0.202 0.370 0.498 0.650 0.795 0.858 0.910
8 0.190 0.350 0.666 0.819 0.879 0.929
9 0.219 0.399 0.578 0.699 0.709 0.822 0.953
10 0.255 0.400 0.545 0.690 0.796 0.825 0.836
11 0.224 0.381 0.577 0.756 0.869 0.929 0.999
12 0.187 0.329 0.441 0.525 0.621 0.796
13 0.278 0.471 0.606 0.770 0.888 1.001 1.105
;
RUN

 

 

I did this as my code but still getting errors. What am I doing wrong? Sorry I am very new to this

Tom
Super User Tom
Super User

Did you get any instructions on how to make a data step? How to use the INPUT statement?

Here is an article about it:  https://support.sas.com/resources/papers/proceedings/proceedings/sugi29/253-29.pdf

Here is the first example in that article:

DATA LIST;
 INPUT X Y A $ Z;
DATALINES;
1 2 HELLO 3
4 5 GOODBYE 6
;
PROC PRINT DATA=LIST;
 TITLE 'LIST INPUT';
RUN; 

Notice how there is a list of names on the INPUT statement?  Notice the DATALINES statement  that marks the start of the in-line data?  (your original code was attempting to use CARDS statement, which is the older alias for the same thing.).  Notice how each statement ends with a semi-colon?  Notice how the end of the in-line data is marked by a line that only has a semi-colon?

 

Krueger
Pyrite | Level 9

You're mixing up PROC IMPORT with a Data step. PROC IMPORT is done as my previous example above. What you've done here is closer to a DATALINES step except your missing that key syntax.

 

Below is what you need to do (keep in mind this isn't 100% working due to missing data).

 

/*naming our dataset */
data have; 
/* Setting input to declare our variables (column headers) */
input Mouse Day3 Day6 Day9 Day12 Day15 Day18 Day21;
/* calling datalines which allows us to put in the values */
datalines;
 1 0.109 0.388 0.621 0.823 1.078 1.132 1.191
 2 0.218 0.393 0.568 0.729 0.839 0.852 1.004
 3 0.394 0.549 0.700 0.783 0.870 0.925	    
 4 0.209 0.419 0.645 0.850 1.001 1.026 1.069
 5 0.193 0.362 0.520 0.530 0.641 0.640 0.751
 6 0.201 0.361 0.502 0.530 0.641 0.640 0.751
 7 0.202 0.370 0.498 0.650 0.795 0.858 0.910
 8 0.190 0.350 0.666 0.819 0.879 0.929      
 9 0.219 0.399 0.578 0.699 0.709 0.822 0.953
10 0.255 0.400 0.545 0.690 0.796 0.825 0.836
11 0.224 0.381 0.577 0.756 0.869 0.929 0.999
12 0.187 0.329 0.441 0.525 0.621 0.796	 	
13 0.278 0.471 0.606 0.770 0.888 1.001 1.105
;
RUN

When you look over the above and run it you should notice that your data doesn't line up starting at Mouse 4. Given Day3-Day21 are all numeric values you need to identify the blanks as missing. To do that you need to place a period for those areas.

 

/*naming our dataset */
data have;
/* Setting input to declare our variables (column headers) */
input Mouse Day3 Day6 Day9 Day12 Day15 Day18 Day21;
/* calling datalines which allows us to put in the values */
datalines;
1 0.109 0.388 0.621 0.823 1.078 1.132 1.191
2 0.218 0.393 0.568 0.729 0.839 0.852 1.004
3 0.394 0.549 0.700 0.783 0.870 0.925 .
4 0.209 0.419 0.645 0.850 1.001 1.026 1.069
5 0.193 0.362 0.520 0.530 0.641 0.640 0.751
6 0.201 0.361 0.502 0.530 0.641 0.640 0.751
7 0.202 0.370 0.498 0.650 0.795 0.858 0.910
8 0.190 0.350 0.666 0.819 0.879 0.929 .
9 0.219 0.399 0.578 0.699 0.709 0.822 0.953
10 0.255 0.400 0.545 0.690 0.796 0.825 0.836
11 0.224 0.381 0.577 0.756 0.869 0.929 0.999
12 0.187 0.329 0.441 0.525 0.621 0.796 .
13 0.278 0.471 0.606 0.770 0.888 1.001 1.105
;
RUN/*naming our dataset */ data have; /* Setting input to declare our variables (column headers) */ input Mouse Day3 Day6 Day9 Day12 Day15 Day18 Day21; /* calling datalines which allows us to put in the values */ datalines; 1 0.109 0.388 0.621 0.823 1.078 1.132 1.191 2 0.218 0.393 0.568 0.729 0.839 0.852 1.004 3 0.394 0.549 0.700 0.783 0.870 0.925 . 4 0.209 0.419 0.645 0.850 1.001 1.026 1.069 5 0.193 0.362 0.520 0.530 0.641 0.640 0.751 6 0.201 0.361 0.502 0.530 0.641 0.640 0.751 7 0.202 0.370 0.498 0.650 0.795 0.858 0.910 8 0.190 0.350 0.666 0.819 0.879 0.929 . 9 0.219 0.399 0.578 0.699 0.709 0.822 0.953 10 0.255 0.400 0.545 0.690 0.796 0.825 0.836 11 0.224 0.381 0.577 0.756 0.869 0.929 0.999 12 0.187 0.329 0.441 0.525 0.621 0.796 . 13 0.278 0.471 0.606 0.770 0.888 1.001 1.105 ; RUN

 

Tom
Super User Tom
Super User

So now your INPUT statement is not reading any variables and all. That is actually a valid statement, but not what you need.

 

Also there is nothing to tell the data step that the lines starting with MOUSE are not statements.  But they don't look like SAS statements do they?

harveysarah0
Calcite | Level 5

So what is wrong?

andreas_lds
Jade | Level 19

@Krueger posted a working version of proc import reading the file. Have your tried it? The only thing you need to do, is changing the path to the location where you have saved the txt-file.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 2576 views
  • 0 likes
  • 4 in conversation