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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.