Help using Base SAS procedures

UPDATE PROBLEM

Accepted Solution Solved
Reply
Regular Contributor
Regular Contributor
Posts: 238
Accepted Solution

UPDATE PROBLEM

I am really not doing this right. I have this code

proc sql;

update table

set ((desc = n/a if cd is null) and (cd = 0 if desc = n/a) and

(desc = minor if cd = 1) and (desc = moderate if cd =2) and

(desc = major if cd = 3) and (desc = extreme if cd = 4));

run;

I think my code is just not right


Accepted Solutions
Solution
‎03-19-2012 02:46 PM
Respected Advisor
Posts: 3,156

Re: UPDATE PROBLEM

Try this:

proc sql;

update table yourtablename

set desc=

          case

               when cd is null

then 'n/a'

                when cd=1

then 'minor'

when cd=2

then 'moderate'

when cd=3

then 'major'

when cd=4

then 'extreme'

else 'others'

end;

update table want

  set cd=0 where desc='n/a';

quit;

View solution in original post


All Replies
Solution
‎03-19-2012 02:46 PM
Respected Advisor
Posts: 3,156

Re: UPDATE PROBLEM

Try this:

proc sql;

update table yourtablename

set desc=

          case

               when cd is null

then 'n/a'

                when cd=1

then 'minor'

when cd=2

then 'moderate'

when cd=3

then 'major'

when cd=4

then 'extreme'

else 'others'

end;

update table want

  set cd=0 where desc='n/a';

quit;

Regular Contributor
Regular Contributor
Posts: 238

UPDATE PROBLEM

I get errors saying: expressions using equals (=) has components that are different data types

Respected Advisor
Posts: 3,156

Re: UPDATE PROBLEM

You need to let us know the variable type of your variables: cd and desc. is 'cd' numeric?

You find the info by running:

1. proc sql;

describe table yourtablename;

quit;

or

2. proc contents data=your tablename;

run;

After knowing the types of your variables, you will be able to make changes.

Regards,

Haikuo

Regular Contributor
Regular Contributor
Posts: 238

UPDATE PROBLEM

They are both text fields

Respected Advisor
Posts: 3,156

UPDATE PROBLEM

Well, then:

proc sql;

update table yourtablename

set desc=

           case

                when cd is null

  then 'n/a'

                 when strip(cd)='1'

  then 'minor'

  when strip(cd)='2'

  then 'moderate'

  when strip(cd)='3'

  then 'major'

  when strip(cd)='4'

  then 'extreme'

  else 'others'

  end;

  update table want

   set cd='0' where desc='n/a';

quit;

Regular Contributor
Regular Contributor
Posts: 238

UPDATE PROBLEM

I got it to work by putting ' ' around the 0-4 and it worked

Respected Advisor
Posts: 3,156

UPDATE PROBLEM

There you go!

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 206 views
  • 0 likes
  • 2 in conversation