BookmarkSubscribeRSS Feed
smart
Fluorite | Level 6

Please help me with creating the SAS code from the following statements, I am not sure where to start: 

 

 

 

A SAS data set called survey2011is stored in the '/courses/ddb976e5ba27fe300/PSY6560/' folder in SAS Studio.

 

Creates a temporary data set called cleansurvey2011. The cleansurvey2011 data set should be based on the survey2011data set, but should have the following additional characteristics.

 

  • The possible values for the demographic questions are 0 and 1. Any other value should be recoded as missing data. Consider using the notin( ) function.

 

  • The possible values for the anxiety questions are 1, 2, and 3. Any other value should be recoded as missing data.

 

  • The possible values for the depression questions are 1, 2, 3, 4, 5, 6, and 7. Any other value should be recoded as missing data.

 

  • Reverse score the following depression questions: 3, 4, 8, 9, 10, and 11. A quick way to reverse code a variable is: reversed = scalemax+1  – original. For example,

 

anxiety1r = 4 – anxiety1;

 

  • Create an average anxiety score for each person.

 

  • Create an average depression score for each person.

 

  • Assigns permanent labels and formats to each demographic variable.

 

Variable Name

 

Dem1

Sex (0 = Male, 1 = Female)

Dem2

Do you reside in Rutherford County? (0 = No, 1 = Yes)

Dem3

Are you married? (0 = No, 1 = Yes)

Dem4

Do you have children? (0 = No, 1 = Yes)

Dem5

Do you own your own home? (0 = No, 1 = Yes)

Dem6

Do you own a vehicle? (0 = No, 1 = Yes)

Dem7

Do you work full-time? (0 = No, 1 = Yes)

Dem8

Are you currently enrolled at MTSU? (0 = No, 1 = Yes)

 

  • Print descriptive statistics for average anxiety and average depression scores.

 

  • Print frequencies for the demographic variables.
11 REPLIES 11
ballardw
Super User

This looks a lot like homework for a class. Has the instructor covered anything about a data step code block in SAS? That would be the likely intended tool. Do you know what a LIBNAME statement is? If not that might be the first place as that associates a storage location with some thing SAS can use to read or write data sets.

 

What have you attempted so far?

smart
Fluorite | Level 6

It is from a class, it's a review of DO Loops and Arrays and I don't understand these concepts. All I have so far is the libname statement:

 

libname mtsu '/courses/ddb976e5ba27fe300/PSY6560/';
run;

Shmuel
Garnet | Level 18

Before coding a loop with arrays check does next code run without errors in log:

  libname mydata   '/courses/ddb976e5ba27fe300/PSY6560/' ;

 

if there are errors it means that you need some more instructions how to deal with the data.

If no errors found run next code:

     data  work.mycopy;

       set mydata. survey2011 ;

           /*  here should be added the code to deal with the data */

    run;

 

If the code runs without error, try to add the desired code and post your results: the code and the log.

smart
Fluorite | Level 6

Thank you! I understand libname, but array and DO loops are new to me. 

Shmuel
Garnet | Level 18

Start with google on  sas documentation  on ARRAY and  DO  staements.

 

First of all try to write your own code.

The forum will help you to understand the errors and how to fix them.

ballardw
Super User

@smart wrote:

Thank you! I understand libname, but array and DO loops are new to me. 


Arrays are basically a short hand way of doing the same thing to a bunch of variables. One way to think of an array is like a file cabinet.

You put the value of one variable into each drawer of the cabinet with the array statement. Then you put the value from drawer 1 and examine and manipulate, then go to drawer 2, then 3, repeat until you run out of drawers.

 

One common idea with arrays is to group similar valued variables into a single array. Since your questions show different coding rules that gives you an idea how many different arrays you may need.

 

One hint that may help:

Type this code into your SAS editor:

Data work.junk;

     array x{10};

run;

then place your cursor on the word array and press F1. You may get a popup taking you to the documentation of the array statement which has several basic examples of declaring and using arrays.

 

Do you know how to write the code to modify one of the variables? If so, use one of the above array examples to manipulate your data just using array definition with your variables and the modification code.

Just work on one coding rule/array at a time. When the first one works you should find the others easy.

 

And the LABEL portion should be trivial if you read the documentation for LABEL.

Cynthia_sas
SAS Super FREQ

Hi:
It looks like you are using SAS Studio with SAS OnDemand for Academics. The location you've posted is a course folder assigned to a professor.

The first thing you do is issue a LIBNAME statement to point to the data and then go from there. Possibly a PROC CONTENTS and then a PROC FREQ. I'd throw in a PROC PRINT, once I started changing things to be sure my code worked. Something like this:

soda_where_start_have_data.png

That code worked for me in SAS OnDemand for Academics, so I think you should give it a try.

 

For some examples of working with DO Loops and Arrays in a program, here's a good place to start: https://support.sas.com/rnd/papers/sgf07/arrays1780.pdf

Cynthia

Reeza
Super User
I think this covers everything you need for your assignment and has fully worked examples. I would recommend working through it first and then attempting your homework. It's ultimately faster that way.
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
smart
Fluorite | Level 6

Here is my attempted code for the assignment- I wasn't sure how to assign permanent variables so I didn't start that yet. Please help me, I know I am doing this wrong.

 

libname mtsu '/courses/ddb976e5ba27fe300/PSY6560/';
run;

data cleansurvey2011;
set mtsu.survey2011;
array dem [8];
array anx [50];
array dep [18];
do i = 1 to 8;
do i = 8 to 50;
do i = 50 to 68;
if dem[i] = . then dem[i] = (missing data);
if anx[i] = . then anx[i] = (missing data);
if dep[i]= . then dep[i] = (missing data);
depreversed = dep18+1 - dep1;
end;
end;
end;
avg = anx;
avg = dep;

proc contents data = mtsu.survey2011;
run;
proc freq data = mtsu.survey2011;
tables dem;
run;
proc print data = cleansurvey2011;
var anx dep;
run;

Reeza
Super User
Add some comments to your code please, to indicate what you think is happening and which question you’re answering.
smart
Fluorite | Level 6

libname mtsu '/courses/ddb976e5ba27fe300/PSY6560/';
run;

/* Creates a temporary data set called cleansurvey2011. */
/* The cleansurvey2011 data set should be based on the survey2011 data set, */
/* but should have the following additional characteristics. */
data cleansurvey2011;
set mtsu.survey2011;
/* The possible values for the demographic questions are 0 and 1. */
/* Any other value should be recoded as missing data. Note. */
/* Consider using the notin( ) function. */
array dem [8];
do i = 1 to 8;
if dem[i] = . then dem[i] = (missing data);
/* The possible values for the anxiety questions are 1, 2, and 3. */
/* Any other value should be recoded as missing data. */
array anx [50];
do i = 8 to 50;
if anx[i] = . then anx[i] = (missing data);
/* The possible values for the depression questions are 1, 2, 3, 4, 5, 6, and 7. */
/* Any other value should be recoded as missing data. */
array dep [18];
do i = 50 to 68;
if dep[i]= . then dep[i] = (missing data);
/* Reverse score the following depression questions: 3, 4, 8, 9, 10, and 11. A quick way to reverse code a variable is: */
/* reversed = scalemax+1 – original */
depreversed = dep18+1 - dep1;
end;
end;
end;
/* Create an average anxiety score for each person. */
avg = anx;

/* Create an average depression score for each person. */
avg = dep;
/* Assigns permanent labels and formats to each demographic variable. */
if Dem1 = 0 then Feedback = 'Male';
else if Dem1 = 1 then Feedback = 'Female';
if Dem2 = 0 then Feedback = 'No';
else if Dem2 = 1 then Feedback = 'Yes';
if Dem3 = 0 then Feedback = 'No';
else if Dem3 = 1 then Feedback = 'Yes';
if Dem4 = 0 then Feedback = 'No';
else if Dem4 = 1 then Feedback = 'Yes';
if Dem5 = 0 then Feedback = 'No';
else if Dem5 = 1 then Feedback = 'Yes';
if Dem6 = 0 then Feedback = 'No';
else if Dem6 = 1 then Feedback = 'Yes';
if Dem7 = 0 then Feedback = 'No';
else if Dem7 = 1 then Feedback = 'Yes';
if Dem8 = 0 then Feedback = 'No';
else if Dem8 = 1 then Feedback = 'Yes';

/* Print descriptive statistics for average anxiety and average depression scores. */
/* */
/* Print frequencies for the demographic variables. */
/* */
proc contents data = mtsu.survey2011;
run;
proc freq data = mtsu.survey2011;
tables dem;
run;
proc print data = cleansurvey2011;
var anx dep;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 1661 views
  • 1 like
  • 5 in conversation