BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mlogan
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
Miracle
Barite | Level 11

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

3 REPLIES 3
Miracle
Barite | Level 11

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;

mlogan
Lapis Lazuli | Level 10

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;

ballardw
Super User

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';

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 choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

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