BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cjoneckis10
Calcite | Level 5

I have about 29 character variables in a dataset that need to be changed depending on which column it is in.  I know how to list all the IF statement like below, but would like to do it different as it will help in similar projects in the future.  So if the values are not equal to zero then I need to change the values depending on which column it is in.  

 

for example

 

DATA HAVE;

LENGTH F1 $2 F2 $2;

INPUT F1 $ F2 $

DATALINES;

00 00

10 12

00 12

10 00

;

RUN;

 

DATA WANT;

SET HAVE;

IF FR1 ^= '00' THEN FR1 = '05';

IF FR2 ^= '00' THEN FR2 = '36';

RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Like this?

data WANT;         
  set HAVE;
  array NEW [2] $2 _temporary_ ('05','36');
  array COL [2] F1-F2;
  do i =1 to 2;
    if COL[I]='00' then COL[I]=NEW[I];
  end;
run;

 

View solution in original post

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

Like this?

data WANT;         
  set HAVE;
  array NEW [2] $2 _temporary_ ('05','36');
  array COL [2] F1-F2;
  do i =1 to 2;
    if COL[I]='00' then COL[I]=NEW[I];
  end;
run;

 

cjoneckis10
Calcite | Level 5

Thank you!  that works

ballardw
Super User

Another approach is to move the If/then logic to a format.

Proc format library=work;
value $F1_
'00' = '00'
other= '05'
;
value $F2_
'00' = '00'
other= '36'
;

DATA HAVE;
LENGTH F1 $2 F2 $2;
INPUT F1 $ F2 $ ;
DATALINES;
00 00
10 12
00 12
10 00
;
RUN;
 
DATA WANT;
SET HAVE;
   f1 = put(f1,$f1_.);
   f2 = put(f2,$f2_.);
RUN;

/* or use the format without changing values*/
Proc print data=have;
format f1 $f1_. f2 $f2_.;
run;

Format names can't end in a digit so I used the _.

This has some flexibility because you said "it different as it will help in similar projects in the future.".

The values assigned by a format can be used by almost any analysis, graphing or reporting procedure. So if you have multiple values that need the '00' or '36' coding you don't really need to modify the data sets but can just apply the format when needed.

Or change the coding for any one variable to pick different coding as needed. Suppose your F1 variable would in some case (given more values than '00' and '10' ) like to have 3 report groups. If you actually change the value then you would have to retrace the to an earlier form of the data set before doing the recoding. But if you leave the original values alone then using a different format on the original value uses the variable as desired.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 3872 views
  • 0 likes
  • 3 in conversation