DATA Step, Macro, Functions and more

Add information to an existing dataset in SAS

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Add information to an existing dataset in SAS

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,

 

 

 


Accepted Solutions
Solution
‎08-29-2016 04:43 PM
Trusted Advisor
Posts: 1,395

Re: Add information to an existing dataset in SAS

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


All Replies
Super User
Posts: 10,516

Re: Add information to an existing dataset in SAS

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.

 

New Contributor
Posts: 3

Re: Add information to an existing dataset in SAS

Thanks for your anaswer

 

Solution
‎08-29-2016 04:43 PM
Trusted Advisor
Posts: 1,395

Re: Add information to an existing dataset in SAS

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;

New Contributor
Posts: 3

Re: Add information to an existing dataset in SAS

Thanks for the code. I believe the "WANT" data is the data holding the continent. is that correct?
Trusted Advisor
Posts: 1,395

Re: Add information to an existing dataset in SAS

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

Super User
Super User
Posts: 6,502

Re: Add information to an existing dataset in SAS

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;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 275 views
  • 1 like
  • 4 in conversation