BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zimcom
Pyrite | Level 9

Good morning!

 

I have a table 

CountrySexAge
CanadaFemale89
CanadaMale62
USAFemale55

 

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 !

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:

  Here's a solution using PROC REPORT with the NEWLINE function and ODS ESCAPECHAR set to '^':

Cynthia_sas_0-1598033029675.png

Hope this helps,

Cynthia

View solution in original post

11 REPLIES 11
PeterClemmensen
Tourmaline | Level 20

Is this for reporting purposes or do you need at SAS data set like this?

zimcom
Pyrite | Level 9
Yes, for reporting purpose.
Shmuel
Garnet | Level 18
data _null_;
 set have;
     file print;
     if _N_ = 1 then put "Country/Sex/Age";
     put country / Sex / Age;
end;
zimcom
Pyrite | Level 9
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;
ballardw
Super User

@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.

zimcom
Pyrite | Level 9

 

Have

PIDCountrySexAgeOutcome
123CanadaFemale89Good
456CanadaMale62Good
789USAFemale55Best

 

Want

PIDCountry/Sex/AgeOutcome
123Canada
Female
89
Good
456Canada
Male
62
Good
789USA
Female
55
Best

 

zimcom
Pyrite | Level 9
Thank you very much!!
Shmuel
Garnet | Level 18

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.

zimcom
Pyrite | Level 9
wgat would be the variable name for "Country/Sex/Age" then?
Cynthia_sas
SAS Super FREQ

Hi:

  Here's a solution using PROC REPORT with the NEWLINE function and ODS ESCAPECHAR set to '^':

Cynthia_sas_0-1598033029675.png

Hope this helps,

Cynthia

Shmuel
Garnet | Level 18

@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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 11 replies
  • 1411 views
  • 1 like
  • 5 in conversation