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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1510 views
  • 0 likes
  • 2 in conversation