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

I have a string variable region. The first two characters are the region name followed by either _rural or _urban, such as:

AN_rural

AN_urban

AR_rural

AR_urban

etc.

 

For observations in a rural region (all those ending by _rural), when a condition is meet (in short, with a random number is lower than a rate), I want to replace _rural by _urban. It should look like:

 

if substr(region,4,5)='rural' then do;

 if rate<rand('uniform') then (replace _rural by_urban);

end;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mklangley
Lapis Lazuli | Level 10

The TRANWRD function can be used to replace occurrences of a given word in a string. For your scenario, for qualifying regions, you can replace 'rural' with 'urban'.

 

Try this, for an example. Compare the before (have) and after (want) datasets.

data have;
    input region $ rate;
    datalines;
AN_rural 1
AD_rural 2
AR_rural 3
AS_rural 4 
BR_rural 5
CH_rural 6
CT_rural 7
DN_rural 8
DD_rural 9
DL_rural 10
GA_rural 11
GJ_rural 12
HR_rural 13
HP_rural 14
JK_rural 15
JH_rural 16
    ;
run;

data want;
    set have;
    if substr(region, 4, 5) = 'rural' then do;
        if rate < 10  /*you can modify this to your random number condition*/
            then region = tranwrd(region, 'rural', 'urban');
    end;
run;

View solution in original post

4 REPLIES 4
utrocketeng
Quartz | Level 8

from reading your request, it seems as though you have a column for region and then a column for observations.  based on what it sounds like you are trying to do, i would split the observations by region (you will end up with the observations as values under the columns for urban vs rural).  then you could use the case statement you are proposing ... note that you may want research the case statement as you likely will not need a replace function.

 

i think something like this might work:

 

if AN_rural <rand('uniform') then do;

 AN_urban;

end;

else do;

AN_rural;

end;

 

without knowing more, the above is a guess and will surly need some work.

Demographer
Pyrite | Level 9

That would work of course, but I have 35 regions (see below), so I would need to repeat this 35 times. Ideally, I would like to have a code that would just replace the "rural" by "urban"

 

AN_urban
AD_urban
AR_urban
AS_urban
BR_urban
CH_urban
CT_urban
DN_urban
DD_urban
DL_urban
GA_urban
GJ_urban
HR_urban
HP_urban
JK_urban
JH_urban
KA_urban
KL_urban
LD_urban
MP_urban
MH_urban
MN_urban
ML_urban
MZ_urban
NL_urban
OR_urban
PY_urban
PB_urban
RJ_urban
SK_urban
TN_urban
TR_urban
UT_urban
UP_urban
WB_urban
AN_rural
AD_rural
AR_rural
AS_rural
BR_rural
CH_rural
CT_rural
DN_rural
DD_rural
DL_rural
GA_rural
GJ_rural
HR_rural
HP_rural
JK_rural
JH_rural
KA_rural
KL_rural
LD_rural
MP_rural
MH_rural
MN_rural
ML_rural
MZ_rural
NL_rural
OR_rural
PY_rural
PB_rural
RJ_rural
SK_rural
TN_rural
TR_rural
UT_rural
UP_rural
WB_rural

 

 

mklangley
Lapis Lazuli | Level 10

The TRANWRD function can be used to replace occurrences of a given word in a string. For your scenario, for qualifying regions, you can replace 'rural' with 'urban'.

 

Try this, for an example. Compare the before (have) and after (want) datasets.

data have;
    input region $ rate;
    datalines;
AN_rural 1
AD_rural 2
AR_rural 3
AS_rural 4 
BR_rural 5
CH_rural 6
CT_rural 7
DN_rural 8
DD_rural 9
DL_rural 10
GA_rural 11
GJ_rural 12
HR_rural 13
HP_rural 14
JK_rural 15
JH_rural 16
    ;
run;

data want;
    set have;
    if substr(region, 4, 5) = 'rural' then do;
        if rate < 10  /*you can modify this to your random number condition*/
            then region = tranwrd(region, 'rural', 'urban');
    end;
run;
AMSAS
SAS Super FREQ

Here's some sample code that uses the SUBSTR right of and SUBSTR left of function

data got ;
	input var $ ;
	do i=1 to 10 ;
		output ;
	end ;
cards ;
AN_rural
AN_urban
AR_rural
AR_urban
;
run ;

data want ;
	set got ;
	/* Check for "rural" */
	if substr(var,4,5)="rural" then
	do ;
		/* If random number < 0.5 */
		if ranuni(0)<0.5 then
		do ;
			put "Before : " var ;
			/* change rural to urban */
			substr(var,4,5)="urban" ;
			put "After : " var ;
		end ;
	end ;
run ;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 516 views
  • 0 likes
  • 4 in conversation