BookmarkSubscribeRSS Feed
mahfuz_pops
Calcite | Level 5

hi, actually it would be great if I can have code for the following probem

I have a large no. of variable but I'm just presenting for few. suppose I have a file named A of the following form

      s    e     d   v1    v2    v3    v4   v5    v6

1    2    6     2    1

2    3    5     4    1     1                            1

3                       1     1      1      1            1

4    2    3     5    1                     1     1     1

 

In the above data file, red colored numbers are just the serial number, and blank spaces means missing variables

Now I want to put 5 in the variables starting from s=2 and ending at e=6, e.i. starting from v2 to v6, if the variable d is greater than 1. But I want the columns s & e as the index of starting and ending of entry, I don't want the incorporation of variables directly, so that I can use this programme for a very large number of variables and cases, and for that the entry of in the data file depends on the s and e variables. e.g. If s is 2 then the recoding should start from v2 and if e is 6 the recoding should end at v6. Now I want my new table looks like as follows:

 

 

      s    e     d   v1    v2    v3    v4   v5    v6

1    2    6     2    1     5     5      5     5      5

2    3    5     0    1     1                            1

3                       1     1     1      1             1

4    2    3     5    1     5     5      1     1      1

 

This means that, the previous entries in the table remains as before and just put in the required fields.

It would be of great healp if you send me the code for doing this.

 

 

2 REPLIES 2
PGStats
Opal | Level 21

Use an array :

 

data have;
input       
s    e     d   v1    v2    v3    v4   v5    v6;
datalines;
2    6     2    1     .     .     .    .     .
3    5     0    1     1     .     .    .     1
.    .     .    1     1     1     1    .     1
2    3     5    1     .     .     1    1     1
;

data want;
set have;
array _v{*} v:;
if d > 1 then
    do i = max(1,s) to min(dim(_v),e);
        _v{i} = 5;
        end;
drop i;
run;

proc print data=want noobs; run;
PG
mahfuz_pops
Calcite | Level 5

awesome...thanks a lot for the code...

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 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
  • 709 views
  • 1 like
  • 2 in conversation