Dear Community,
I have the following dataset. I would like to have my output dataset to be such a way that the unique observations(location) should become my columns and require a count column to suggest the no of values. Appreciate your inputs.
ID | Location |
101 | UK |
101 | France |
101 | Other |
102 | UK |
102 | Spain |
103 | Other |
104 | UK |
104 | Other |
105 | Other |
I want the dataset to look like
ID | UK | France | Spain | Other | Count |
101 | Y | Y | Y | 3 | |
102 | Y | Y | 2 | ||
103 | Y | 1 | |||
104 | Y | Y | 2 | ||
105 | Y | 1 |
Only if ID is character (it is not in the sample data that I posted).
Otherwise do something like this
data have;
input ID $ Location $;
datalines;
101 UK
101 France
101 Other
102 UK
102 Spain
103 Other
104 UK
104 Other
105 Other
;
proc transpose data = have out = temp(drop = _:);
by ID;
id Location;
var Location;
run;
data want;
set temp;
array a UK -- Spain;
count = 0;
do over a;
a = ifc(a ne '', 'Y', a);
if a = 'Y' then count + 1;
end;
run;
Try this
data have;
input ID Location $;
datalines;
101 UK
101 France
101 Other
102 UK
102 Spain
103 Other
104 UK
104 Other
105 Other
;
proc transpose data = have out = temp(drop = _:);
by ID;
id Location;
var Location;
run;
data want;
set temp;
array a _character_;
count = 0;
do over a;
a = ifc(a ne '', 'Y', a);
if a = 'Y' then count + 1;
end;
run;
Thanks for your input.
However, ID column also gets replaced with "Y".
ID | UK | France | Spain | Other | Count |
Y | Y | Y | Y | 3 | |
Y | Y | Y | 2 | ||
Y | Y | 1 | |||
Y | Y | Y | 2 | ||
Y | Y | 1 |
Only if ID is character (it is not in the sample data that I posted).
Otherwise do something like this
data have;
input ID $ Location $;
datalines;
101 UK
101 France
101 Other
102 UK
102 Spain
103 Other
104 UK
104 Other
105 Other
;
proc transpose data = have out = temp(drop = _:);
by ID;
id Location;
var Location;
run;
data want;
set temp;
array a UK -- Spain;
count = 0;
do over a;
a = ifc(a ne '', 'Y', a);
if a = 'Y' then count + 1;
end;
run;
Thanks for your suggetion.
Except for ID variable, how do I rename the all the transposed location variables with prefix m_?
For example, in my final dataset i want
ID | m_UK | m_France | m_Spain | m_Other |
Use the prefix= option in Proc Transpose like this
data have;
input ID $ Location $;
datalines;
101 UK
101 France
101 Other
102 UK
102 Spain
103 Other
104 UK
104 Other
105 Other
;
proc transpose data = have out = temp(drop = _:) prefix = m_;
by ID;
id Location;
var Location;
run;
data want;
set temp;
array a m_:;
count = 0;
do over a;
a = ifc(a ne '', 'Y', a);
if a = 'Y' then count + 1;
end;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.