SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
love_epi
Calcite | Level 5

Hello,

I am trying to transform a column variable into several dummy variables with one row per ID.

 

Here is the data I have:

ID, Referral Type

1, Housing

1, Transportation

2, Food

3, Housing

3, Food

4, Transportation

4, Mental Health

 

Here is the data I want:

ID, Housing, Transportation, Food, Mental Health

1, 1, 1, 0, 0

2, 0, 0, 1, 0

3, 1, 0, 1, 0

4, 0, 1, 0, 1

 

Any advice would be greatly appreciated! Thanks 🙂

2 REPLIES 2
ballardw
Super User

How do you expect to use that resulting data set?

Your use of "dummy variables" makes me think you may want to use this in some sort of regression. In which case why cause yourself work. Most of the regression procedures support CLASS variables which SAS will use the levels to create dummy variables for you. For most purposes the form of data you have is preferred.

 

Or if you are looking for a report for people to read some thing like this perhaps:
Note use of DATA Step code to provide a usable data set.

 

data have;
  infile datalines dsd dlm=' ';
  input ID Referral :$15.;
datalines;
1 Housing
1 Transportation
2 Food
3 Housing
3 Food
4 Transportation
4 "Mental Health"
;

proc tabulate data=have;
   class id referral;
   table id,
         referral*n=' '
         /misstext='0'
   ;
run;

 

Ksharp
Super User
data have;
  infile datalines dsd dlm=' ';
  input ID Referral $15.;
  var=1;
datalines;
1 Housing
1 Transportation
2 Food
3 Housing
3 Food
4 Transportation
4 Mental Health
;

proc transpose data=have out=temp(drop=_name_);
by id;
id Referral;
var var;
run;
proc stdize data=temp out=want missing=0 reponly;
run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 2 replies
  • 970 views
  • 1 like
  • 3 in conversation