BookmarkSubscribeRSS Feed
FrustratedBio
Calcite | Level 5

I have to get this code into a frequncy table. 

 

Data Ques;
Input ID $ Reason1-Reason4;
Datalines;
001 3 6 13 17
002 8 3 4 .
003 20 2 . .
004 8 4 20 19
;

 

The code format I used and that my textbook suggested to do this goes like:

 

Data Ques;
Input ID $ Reason1-Reason4;
Datalines;
001 3 6 13 17
002 8 3 4 .
003 20 2 . .
004 8 4 20 19
;
Proc Sort Data=Ques;
by ID;
run;

 

Data Ques;
array count[4] Reason1-Reason4;
do Reason=1 to 4;
Dist=count[Reason];
Output;
end;

run;


Proc Print Data=ques;
run;

 

For some reason this doesn't seem to be working for me, and it returns a table full of missing values. The only difference I can perceive from my code and the book's code for the exact same problem is that their example contains a set statement referencing a example dataset. 

3 REPLIES 3
Tom
Super User Tom
Super User

You left out the SET statement so the data step has no data to actually output.

You will be better off if you don't overwrite the original data.  Use a different name on the DATA statement than on the SET statement.

data ques_tall;
  set ques;
  array count[4] Reason1-Reason4;
  do Reason=1 to 4;
    Dist=count[Reason];
    output;
  end;
run;
proc print data=ques_tall;
run;

You can skip the ARRAY and the extra data step and just read the values into the "tall" structure to begin with.

data ques_tall;
  input ID @;
  do Reason=1 to 4 ;
     input Dist @;
     output;
  end;
datalines;
001 3 6 13 17
002 8 3 4 .
003 20 2 . .
004 8 4 20 19
;
FrustratedBio
Calcite | Level 5
I would definitely use the shortcut, however, my professor requires arrays. I was just curious as the first block of code looks nothing like the second.
FrustratedBio
Calcite | Level 5
*runs* nothing like the second

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 767 views
  • 0 likes
  • 2 in conversation