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

I ran a proc contents with an out= to create a dataset that has all of the variables in a single column.  From here I want to be able to perform a handful of transformations based on the middle position of said variables, they look like this:

tml021success

tml021datainform

...there are approx 100 of these vars, and I only want to transform into new vars those vars that have an '046' or '021' as their substr(name,4,3)

the transformation is to make tml021success (1 thru 2=0)  (3 thru 4 = 1) 5=sysmis into ra_tml021success.

questions is:  What proc do I use (or macro) to make the program loop through and identify those vars with a specific substring and recode them into newly created vars?

Any help much appreciated.

Novice sas user

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You have two questions here.

1) How to recode one of these variables.

2) How to simplify identifying all of them and generating the code to re code them.

One way to generate the list of variable names needed to define an array is to use PROC SQL to make a macro variable with the space delimited list of variable names.

proc sql noprint ;

  select name , 'ra_'||name

    into : original separated by ' '

       , : recoded separated by ' '

    from contents

    where substr(name,4,3) in ('021','046')

  ;

quit;

One simple way to apply the same logic to multiple variables is to use an ARRAY ( or in this case two synchronized arrays).

data want ;

  set have ;

  array _a &original;

  array _b &recoded ;

  do over _a ;

    if 1 <= _a <= 2 then _b = 0 ;

    else if 3 <= _a <= 4 then _b=1 ;

    else _b=.;

  end;

run;

View solution in original post

2 REPLIES 2
ballardw
Super User

You don't have to modify any data for most purposes. A custom format such as

Proc format library=work;

value MyBin

1,2 = '0' /* or you can use False, No or whatever 0 actually means to you in place of 0*/

3,4 = '1' /* or True, Yes or whatever*/

5= .

;

run;

Then in analysis procedures or any display associate the format with the variable.

proc freq data=yourdatasetname;

tables tml021success;

format tml021success MyBin. ;

run;

One advantage of this approach is that you can have multiple formats that can be assigned to the same variables for different types of analysis and don't have to create multiple variables such as if you wanted to see how groups based on 1, 2 and 3, 4.

Tom
Super User Tom
Super User

You have two questions here.

1) How to recode one of these variables.

2) How to simplify identifying all of them and generating the code to re code them.

One way to generate the list of variable names needed to define an array is to use PROC SQL to make a macro variable with the space delimited list of variable names.

proc sql noprint ;

  select name , 'ra_'||name

    into : original separated by ' '

       , : recoded separated by ' '

    from contents

    where substr(name,4,3) in ('021','046')

  ;

quit;

One simple way to apply the same logic to multiple variables is to use an ARRAY ( or in this case two synchronized arrays).

data want ;

  set have ;

  array _a &original;

  array _b &recoded ;

  do over _a ;

    if 1 <= _a <= 2 then _b = 0 ;

    else if 3 <= _a <= 4 then _b=1 ;

    else _b=.;

  end;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 863 views
  • 3 likes
  • 3 in conversation