BookmarkSubscribeRSS Feed
PhillipSherlock
Obsidian | Level 7

Hi all,

I'd like to set a conditional statement such that if the first character of column "B" is a number, the values of columns "A" and "B" should be switched.  Any suggestions would be greatly appreciated.

Best,

Phil

2 REPLIES 2
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

Suggest using a DATA step where SAS assignment statements use the ANYDIGIT along with a H_xxxx (hold variable) for your conditional assignment.

What is unspecified here is whether your objective is on an SAS observation level basis or if only one occurrence is different, then switch all observations -- there you might consider a DATA step to test values, then a conditional PROC DATASETS to effect a change on the entire dataset.

35758 - New "ANY" functions in SAS® 9 and later releases can search for various types of characters ...

Scott Barry
SBBWorks, Inc.

Cynthia_sas
SAS Super FREQ

Hi, without data it's not entirely clear what you want. I read this as you want the "new" B value to become the value for A, IF the first character of "old B" value is a number/digit and you want to switch the A value to the "new" B?

Like this:

forum_switch_values.png

Is this what you meant? If so, assuming the data (WORK.FAKEDATA) shown in the first PROC PRINT, this program using ANYDIGIT and SUBSTR does the swap. You can look in the doc to find out what those functions do, as well as looking up the RENAME= option. Of course, you may need to tweak the program based on the real names and lengths of your real variables. The Final dataset is called WORK.SWITCH.

Cynthia


data switch;
  set fakedata(rename=(b=oldb));
  if anydigit(substr(oldb,1,1)) = 1 then do;
     b = a;
     a=oldb;
  end;
  else do;
     b = oldb;
  end;
run;

  

proc print data=switch;
  title 'Switched data (can drop OLDB var after verifying)';
  var a b oldb;
run;
title;

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 3344 views
  • 0 likes
  • 3 in conversation