DATA Step, Macro, Functions and more

Create new variables failed with if/then

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

Create new variables failed with if/then

Hi,

I have a excel file like below:

ID wg wgunit   hg   hgunit

1 203    Lb      64.5 In (12 inches = 1 foot)

1 202.4 Lb      64.5 In (12 inches = 1 foot)

1 198    Lb      64    In (12 inches = 1 foot)

1 184    Lb      64    In (12 inches = 1 foot)

1 95      Kg      64    In (12 inches = 1 foot)

2 188    Lb      67    In (12 inches = 1 foot)

2 174.8 Lb     67    In (12 inches = 1 foot)

I want to make variable with same unit. Here is my syntax:

data a;

  set b;

  weightkg=wg;

  if wgunit='lb' then weightkg=wg*0.4535924;

  heightm=hg/100;

  if hgunit='in' then heightm=hg*2.5/100;

run;

but output showed:

weightkg heightm

203.000  0.6450

202.400  0.6450

198.000  0.6400

184.000  0.6400

95.000   0.6400

188.000 0.6700

174.800 0.6700

I tried to debug my syntax, but it doesn't work.

Thanks for helping me.


Accepted Solutions
Solution
‎10-29-2014 08:34 PM
Trusted Advisor
Posts: 1,228

Re: Create new variables failed with if/then

I think you are trying something like this

data a;

  set b;

  if wgunit='Lb' then weightkg=wg*0.4535924;

  else weightkg=wg; 

  if hgunit='In' then heightm=hg*2.5/100;

  else heightm=hg/100;

run;

View solution in original post


All Replies
Contributor
Posts: 25

Re: Create new variables failed with if/then

String comparison tests are case sensitive in SAS. "lb" is not equal to "Lb".

Never assume strings are always capitalized uniformly.  Use the UPCASE function.

IF UPCASE(wgunit) = "LB" THEN weightkg=.....

conversely:

IF LOWCASE(wgunit)='lb' THEN weightkg=

Frequent Contributor
Posts: 82

Re: Create new variables failed with if/then

Posted in reply to rickpaulos

I tried all versions of LCASE or UCASE, it showed same result. I am wondering why heightm=0.6 and etc. I need someone to help me figuring out the problem.

Thanks,

Super User
Posts: 19,850

Re: Create new variables failed with if/then

In SAS I believe the proper functions are upcase and lowcase function.

In a SQL query the upper and lower functions also work. 

Contributor
Posts: 25

Re: Create new variables failed with if/then

Duh, yeah. UPCASE.  UCASE is for Visual Basic.

Solution
‎10-29-2014 08:34 PM
Trusted Advisor
Posts: 1,228

Re: Create new variables failed with if/then

I think you are trying something like this

data a;

  set b;

  if wgunit='Lb' then weightkg=wg*0.4535924;

  else weightkg=wg; 

  if hgunit='In' then heightm=hg*2.5/100;

  else heightm=hg/100;

run;

PROC Star
Posts: 7,487

Re: Create new variables failed with if/then

Actually, if your data are EXACTLY as shown, I'd think you'd need something like the following (note: I modified your height calculation):

data b;

  informat hgunit $23.;

  input ID wg wgunit $   hg   hgunit &;

  cards;

1 203    Lb      64.5 In (12 inches = 1 foot)

1 202.4 Lb      64.5 In (12 inches = 1 foot)

1 198    Lb      64    In (12 inches = 1 foot)

1 184    Lb      64    In (12 inches = 1 foot)

1 95      Kg      64    In (12 inches = 1 foot)

2 188    Lb      67    In (12 inches = 1 foot)

2 174.8 Lb     162.56    Centimeters

;

data a;

  set b;

  weightkg=wg;

  if wgunit='Lb' then weightkg=wg*0.4535924;

  else weightkg=wg;

  if hgunit="In (12 inches = 1 foot)" then heightm=hg/39.370;

  else heightm=hg/100;

run;

Frequent Contributor
Posts: 82

Re: Create new variables failed with if/then


Hi Stat@sas and Arthur,

Both ways worked very good and got same result. Many thanks for your expertise.

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 246 views
  • 0 likes
  • 5 in conversation