Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

Creating new Variable value based on current observation

Accepted Solution Solved
Reply
Regular Contributor
Posts: 218
Accepted Solution

Creating new Variable value based on current observation

Hi All,

I have the following data set.

ID  

1

2

3

4

I want to create a new variable call New_ID and give them a corresponding Character value if ID=1-3 then New_ID='USA'; if ID=4 then New_ID='Germany'. Can someone tell me if I can do it without format function.


ID      New_ID  

1       USA

2       USA

3       USA

4       Germany

Thanks,able


Accepted Solutions
Solution
‎07-28-2015 08:36 PM
Regular Contributor
Posts: 228

Re: Creating new Variable value based on current observation

Hi there.

Perhaps you could try this?

data in;

  input ID @@;

  datalines;

  1 2 3 4

  ;

run;

data want;

  set in;

  attrib New_ID length=$10.;

  if ID in(1:3) then New_ID='USA';

  else New_ID='Germany';

run;

proc print noobs; run;

View solution in original post


All Replies
Solution
‎07-28-2015 08:36 PM
Regular Contributor
Posts: 228

Re: Creating new Variable value based on current observation

Hi there.

Perhaps you could try this?

data in;

  input ID @@;

  datalines;

  1 2 3 4

  ;

run;

data want;

  set in;

  attrib New_ID length=$10.;

  if ID in(1:3) then New_ID='USA';

  else New_ID='Germany';

run;

proc print noobs; run;

Regular Contributor
Posts: 218

Re: Creating new Variable value based on current observation

Thanks Wong, It worked perfect. Can you please tell me what should I do if there are 3 category?

I want the New_ID as: value 1 to 6 = Germany, 7 to 11 = USA, 12 to 15 = Germany and missing value (.) = N/A

I wrote the following code and it's not working. Can you please help. Thanks,

data want;

  set in;

  attrib New_ID length=$10.;

  if ID in(7:11) then New_ID='USA';

  if ID in(.) then New_ID='N/A';

else New_ID='Germany';

run;

proc print noobs; run;

Super User
Posts: 11,343

Re: Creating new Variable value based on current observation

You are getting all "Germany" values except for .

because of

if ID in(.) then New_ID='N/A';

else New_ID='Germany';

ANY value of ID at this point other than . will yield "Germany".

You want need:

  if ID in(7:11) then New_ID='USA';

  else if ID in(.) then New_ID='N/A';

else New_ID='Germany';

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 381 views
  • 3 likes
  • 3 in conversation