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

Hi,

 

I have a dataset in SAS which includes person_id, and the country.

 

Person_id, Country

1 USA

2 USA

3, GER

4, DEN

 

How can I add the continent info to its current data set? can I do it using macro? so the resulting data set be like the following:

Person_id, Country, Continent

1 USA NORTH AMERICA

2 USA NORTH AMERICA

3, GER Europe

4, DEN Europe

 

Thanks,

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

you don't need a macro to do it;

 

you can add:

    select (country) ;

          when ('USA') continent = 'NORTH AMERICA';

          when ('GER' 'DEN') continent = 'EUROPE';

          otherwize continent = 'OTHER';

    end;

 

or you can do it by creating a format  to get the continent of the country:

        proc format lib=worl;

               value $continent

                      'USA' = ' NORTH AMERICA'

                      'GER' = 'EUROPE'

                      'DEN' = 'EUROPE'

                       ...

                       otehr = 'Not Defined'

               ;

        run;

 

      data WANT;

       set HAVE;

             length continent $25;

            continent = putc(country, $continent.);

      run;

View solution in original post

6 REPLIES 6
ballardw
Super User

Do you have that content information in a data set? Especially if it has the contry name/abbreviation, then an SQL join would possibly be the easiest. If not, then building such a set may be the first thing to do.

 

tafteh
Calcite | Level 5

Thanks for your anaswer

 

Shmuel
Garnet | Level 18

you don't need a macro to do it;

 

you can add:

    select (country) ;

          when ('USA') continent = 'NORTH AMERICA';

          when ('GER' 'DEN') continent = 'EUROPE';

          otherwize continent = 'OTHER';

    end;

 

or you can do it by creating a format  to get the continent of the country:

        proc format lib=worl;

               value $continent

                      'USA' = ' NORTH AMERICA'

                      'GER' = 'EUROPE'

                      'DEN' = 'EUROPE'

                       ...

                       otehr = 'Not Defined'

               ;

        run;

 

      data WANT;

       set HAVE;

             length continent $25;

            continent = putc(country, $continent.);

      run;

tafteh
Calcite | Level 5
Thanks for the code. I believe the "WANT" data is the data holding the continent. is that correct?
Shmuel
Garnet | Level 18

That's right - WANT is rhe output dataset with the new variable CONTINENT;

Tom
Super User Tom
Super User

This is not a problem where macro code would be very useful.  You do not need to dynamically generate SAS code.  You just need to use normal SAS statements to manipulate your data. First let's make dataset out of your example data.

 

data have;
  input person_id country $ ;
cards;
1 USA
2 USA
3 GER
4 DEN
;

data country2continent ;
  input country $ continent $20. ;
cards;
USA NORTH AMERICA
GER EUROPE
DEN EUROPE
;

 Then we can combine them. For example by using an SQL join.

proc sql ;
  create table want as 
   select a.*,b.continent
   from have a 
   left join country2continent b
   on a.country = b.country
 ;
quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 1068 views
  • 1 like
  • 4 in conversation