DATA Step, Macro, Functions and more

How to use the information from 2 variables and create a new one with it?

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

How to use the information from 2 variables and create a new one with it?

Dear SAS Community.

 

I need your help with this. I have 2 Character Variables "Auftraggeber" and "Manufacturer" and want to create a new Variable "Sponsor" with the information from those variables. The information from the variable "Auftraggeber" is the information I need, but there are a lot of missings in this variable. So if there is a missing in the variable "Auftraggeber" , I want SAS to take the Information from the variable "Manufacturer" then. How can I do this?

Thank you for your help Smiley Happy

 

I tried something like

data work.sponsors;
 set work.sponsors;
 sponsor = Auftraggeber;
 if Auftraggeber ne . then sponsor = Auftraggeber;
 if Auftraggeber = . then sponsor = Manufacturer;
 run;


Accepted Solutions
Solution
‎02-21-2018 10:32 AM
SAS Employee
Posts: 30

Re: How to use the information from 2 variables and create a new one with it?

[ Edited ]
Posted in reply to marysmith

You've very nearly solved this one already! The main issue here is that you're comparing a character variable to the numeric missing value. What you really want is a comparison using " " as your missing value. Here's a small re-write you can use, and since you've already set sponsor to Auftraggeber upfront, the only time you need to worry about Manufacturer is if that value was missing.

 

data work.sponsors;
     set work.sponsors;
     sponsor = Auftraggeber;
     if Auftraggeber eq " " then sponsor = Manufacturer;
run;

 

View solution in original post


All Replies
Solution
‎02-21-2018 10:32 AM
SAS Employee
Posts: 30

Re: How to use the information from 2 variables and create a new one with it?

[ Edited ]
Posted in reply to marysmith

You've very nearly solved this one already! The main issue here is that you're comparing a character variable to the numeric missing value. What you really want is a comparison using " " as your missing value. Here's a small re-write you can use, and since you've already set sponsor to Auftraggeber upfront, the only time you need to worry about Manufacturer is if that value was missing.

 

data work.sponsors;
     set work.sponsors;
     sponsor = Auftraggeber;
     if Auftraggeber eq " " then sponsor = Manufacturer;
run;

 

Contributor
Posts: 41

Re: How to use the information from 2 variables and create a new one with it?

Posted in reply to GinaRepole
Thank you!!!!
Valued Guide
Posts: 525

Re: How to use the information from 2 variables and create a new one with it?

Posted in reply to marysmith

Depending on the type of the variable you need to check . or " " (single, quoted blank). I recommend using the function missing(variable) to find missing values.

 

data work.sponsors;
   set work.sponsors;
   
   if missing(Auftraggeber) then do;
      sponsor = Auftraggeber;
   end;
   else do;
      sponsor = Manufacturer;
   end;
run;

You do have interesting variable names, if you don't want to translate var names, check out the regional groups.

Contributor
Posts: 41

Re: How to use the information from 2 variables and create a new one with it?

Posted in reply to andreas_lds

Thank you!!!!!!

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 108 views
  • 1 like
  • 3 in conversation