Learning SAS? Welcome to the exclusive online community for all SAS learners.

help with code

Reply
New User
Posts: 1

help with code

Good morning

I am new to this board and i am desperate for help. I have been working on this code for 2 weeks trying to get it to work and I an running out of time I am hoping someone can help me figure this out. I am taking a class where we were given  sample code then we had to pick a public data set and 6 variables to work with replace them in the code and get the results. I keep getting errors and I have no idea wht they mean and I just need this code to work if anyone can help I am beyond grateful. Below is the beginning of my code 
I am working with

 

 

*Case Study Question 1 Analytics Code;
LIBNAME _Temp0 "/courses/df9a1bd5ba27fe300/NSFG Female Respondent" access=readonly; * Dr. Boman-Davis Library Name and Course;
run;

*proc contents data=_TEMP0.femrespdata_2011_2013;
*run;

proc freq data= _TEMP0.femrespdata_2011_2013; *(where= ((Method) in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)));
tables Metro SEDNO RSCRRACE VRY1STAG VRY1STSX sex1mthd1;
format Metro Metro. SEDNO Formsexed. RSCRRACE Race. sex1mthd1 Method. VRY1STAG AGE. VRY1STSX DATEFIRSTSEX.;
run;


data work.FSFG13;
set _TEMP0.femrespdata_2011_2013;
if 1 <= sex1mthd1 <= 24 then Method=1;
if 95 <= sex1mthd1 <= 96 then Method=2;
run;
if 0 <= VRY1STAG <= 14 then AGE=1;
if 15 <= VRY1STAG <= 17 then AGE=2;
if 18 <= VRY1STAG <= 19 then AGE=3;
run;
if 380 <= VRY1STSX <= 1340 then DATEFIRSTSEX=1;
if 1341 <= VRY1STSX <= 1365 then DATEFIRSTSEX=2;
if RSCRRACE in (1) then Race=1;
if RSCRRACE in (4) then Race=2;
if RSCRRACE in (5) then Race=3;
if RSCRRACE in (6) then Race=4;
if SEDNO in (1) then Formsexed=1;
if SEDNO in (5) then Formsexed=2;
run;

proc format;
value Formsexed
1 = 'yes'
2 = 'no';
proc format;
value Method
1 = 'Method'
2 = 'No Method';
proc format;
value AGE
1 = '0-14'
2 = '15-17'
3 = '18-19';
proc format;
value Race
1 = 'other race'
2 = 'black'
3 = 'white'
4 = 'hispanic';
proc format;
value Metro
1 = 'Principal City'
2 = 'Other Metro'
3 = 'Not Metro';
run;

 

proc format;
value DATEFIRSTSEX
1 = 'BEFORE 2011'
2 = 'BEFORE SEPT 2013';


proc freq data= _TEMP0.femrespdata_2011_2013; *(where= ((Method) in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)));
tables Metro SEDNO RSCRRACE VRY1STAG VRY1STSX sex1mthd1;
format Metro Metro. SEDNO Formsexed. RSCRRACE Race. sex1mthd1 Method. VRY1STAG AGE. VRY1STSX DATEFIRSTSEX.;
run;

Super User
Posts: 6,928

Re: help with code

Here's a place to start.

 

Your very first PROC FREQ will be filled with errors, because you are attempting to use formats that don't exist.  Later on, you do create the formats, so the final PROC FREQ has a chance at working (as long as earlier errors are cleaned up first).

 

The DATA step does not need multiple RUN statements.  It just needs one RUN statement at the end.  Remove the extra ones.

 

You can test out the program with those changes, and see how far you get.  If you need further help, it's imperative that you post the log, not the program.  That's what informs about the problems.

Super User
Posts: 13,924

Re: help with code

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

The error messages are usually fairly good though sometimes when the issue arises from something missing on a previous line of code such as a semicolon or close parentheses the location of the error may look odd.

 

You want to decide if you want to use a WHERE statement or a data set option. Your code:

*(where= ((Method) in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)));

(Note the appearance of the code box)

 

Is located as if it were a Where statement (after a ; with its own ; to end the statement) but the statement would not use the = or enclose the whole thing in ()as you have.

In either case the variable need not appear as (method) you have it to compare to the list of values. Also the IN operator will allow use of the : to designate a continuous list integers. Either of these should work for the WHERE with Proc Freq:

proc freq data= _TEMP0.femrespdata_2011_2013 (where= (Method in (1:24)));
/*or*/
proc freq data= _TEMP0.femrespdata_2011_2013;
  where Method in (1:24);
Ask a Question
Discussion stats
  • 2 replies
  • 143 views
  • 0 likes
  • 3 in conversation