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

Hi all,

I have data consisting of member ID and 30 variables. The 30 variables are in 3 sets. Basically, a participant can have up to 10 mountains listed. Each mountain will then have another column indicating the country where that mountain is located. Finally, there will be a third column indicating how many times a participant has climbed that mountain.

Like this:

ID     Mountain1       Mountain1_country     Mountain1_climb      Mountain2            Mountain2_country  Mountain2_climb......Mountain10  etc

1   Mount McKinley      US                                   2                Mont Blanc                 France                         4

2   Mount Pico          Portugal                              3                 Mount McKinley          US                              3

What I want is to create columns that sum the climb variables by country--that is, how many times did each participant climb mountains in the US, France, etc.

Any help is much appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Much easier if you convert the data to one row per person-mountain rather on row per person.

I changed your variable names to have the number as the suffix to make it easier to write the code for the example data.

data have;

infile cards dsd dlm='|' truncover ;

length ID 8 Mountain1-Mountain2 $20

Mountain_Country1-Mountain_Country2 $20

Mountain_Climb1-Mountain_Climb2 8

;

input ID Mountain1 Mountain_country1 Mountain_climb1

Mountain2 Mountain_country2 Mountain_climb2

;

cards;

1|Mount McKinley|US|2|Mont Blanc|France|4

2|Mount Pico|Portugal|3|Mount McKinley|US|3

run;

data vertical;

  length id 8 n 8 Mountain $20 Country $20 Climb 8 ;

  set have ;

  array a1 mountain1-mountain2;

  array a2 mountain_country: ;

  array a3 mountain_climb: ;

  do n=1 to dim(a1);

   mountain = a1(n);

    country = a2(n);

    climb = a3(n);

      if not missing(mountain) then output;

  end;

  keep id -- climb ;

run;

proc means nway sum ;

  class country ;

  var climb;

run;

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

Much easier if you convert the data to one row per person-mountain rather on row per person.

I changed your variable names to have the number as the suffix to make it easier to write the code for the example data.

data have;

infile cards dsd dlm='|' truncover ;

length ID 8 Mountain1-Mountain2 $20

Mountain_Country1-Mountain_Country2 $20

Mountain_Climb1-Mountain_Climb2 8

;

input ID Mountain1 Mountain_country1 Mountain_climb1

Mountain2 Mountain_country2 Mountain_climb2

;

cards;

1|Mount McKinley|US|2|Mont Blanc|France|4

2|Mount Pico|Portugal|3|Mount McKinley|US|3

run;

data vertical;

  length id 8 n 8 Mountain $20 Country $20 Climb 8 ;

  set have ;

  array a1 mountain1-mountain2;

  array a2 mountain_country: ;

  array a3 mountain_climb: ;

  do n=1 to dim(a1);

   mountain = a1(n);

    country = a2(n);

    climb = a3(n);

      if not missing(mountain) then output;

  end;

  keep id -- climb ;

run;

proc means nway sum ;

  class country ;

  var climb;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 2371 views
  • 0 likes
  • 2 in conversation