Good morning!
I have a table
Country | Sex | Age |
Canada | Female | 89 |
Canada | Male | 62 |
USA | Female | 55 |
I want to convert into a table below with one variable but combining the 3 variables values into different row
Country/ Sex/ Age |
Canada Female 89 |
Canada Male 62 |
USA Female 55 |
Thanks !
Hi:
Here's a solution using PROC REPORT with the NEWLINE function and ODS ESCAPECHAR set to '^':
Hope this helps,
Cynthia
Is this for reporting purposes or do you need at SAS data set like this?
data _null_;
set have;
file print;
if _N_ = 1 then put "Country/Sex/Age";
put country / Sex / Age;
end;
@zimcom wrote:
I have other variables in the table as well, only want to combine the 3 avriables into one variable, how can I modify the code below:
if _N_ = 1 then put "Country/Sex/Age";
put country / Sex / Age;
The show the variables in the example data and how they are supposed to appear in the output.
Have
PID | Country | Sex | Age | Outcome |
123 | Canada | Female | 89 | Good |
456 | Canada | Male | 62 | Good |
789 | USA | Female | 55 | Best |
Want
PID | Country/Sex/Age | Outcome |
123 | Canada Female 89 | Good |
456 | Canada Male 62 | Good |
789 | USA Female 55 | Best |
Adapting the code to desired output:
data _null_;
set have;
file print;
if _N_ = 1 then put "PID Country/Sex/Age Outcome"; /* as labels */
put @5 country /
@1 PID @5 Sex @15 outcome /
@5 Age;
end;
each @ assigns the starting position of the vale, and / assigns new line.
Hi:
Here's a solution using PROC REPORT with the NEWLINE function and ODS ESCAPECHAR set to '^':
Hope this helps,
Cynthia
@zimcom wrote:
wgat would be the variable name for "Country/Sex/Age" then?
You asked for a report. With the method I used, there is no need to create a new variable, especially as y want to have them vertically, in the same column of the report.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.