Contributor
Posts: 24

# Constructing Variables in a single step

Dear All:

My data is as follows:

Name

A

AA
AAA

AB

ABB

ABC

ACC

And so forth

I want to contruct Variables such that the data set now looks like

Name       Var3      Var5

A                 1           1

AA               1           1
AAA             1           1

AB                             1

ABB                           1

ABC

ACC

It is possible to construct this data set with multiple steps.  But is there a shorter way.

Thanks

Randy

Super User
Posts: 13,889

## Re: Constructing Variables in a single step

What are the rules for assigning those values to the other variables?

Sometimes if you have something like "I have to look at many records with this value for ID and then assign the (max, min, mean something else from the whole set) you may have to summarize the data and then remerge.

But without some idea why a record gets var3 set to 1 and why set to missing anything would be a shot in the dark.

Posts: 1,848

## Re: Constructing Variables in a single step

Are var3 var5 new varaibles to be added ?

What is the logic rules to create those variables?

SAS Super FREQ
Posts: 4,267

## Re: Constructing Variables in a single step

Please show us how you do it with multiple steps so that we can understand your logic.

Contributor
Posts: 24

## Re: Constructing Variables in a single step

data group3 ; set names;

format Gr3 1.

gr3 = 1 ;

run;

data group3 ; set group3;

if _N_ gt 3 then delete;

run;

data group5 ; set names;

format Gr5 1.

gr5 = 1 ;

run;

data group5 ; set group5;

if _N_ gt 5 then delete;

run;

data want;

merge names group3 group5;

by name;

run;

Super User
Posts: 13,889

## Re: Constructing Variables in a single step

You mean like:

```data want;
set names;
if _n_ le 3 then Gr3=1;
if _n_ le 5 then Gr5=1;
run;

```

Note your example code was missing ;

The second data step to subset is redundant

```data group3 ;
set names;
format Gr3 1.   ;
gr3 = 1 ;
if _N_ gt 3 then delete;

run;
```

The forum message windows will sometimes reformat text. It is best to post code and log info into a code box opened with the {i} icon in the menu row at the top of the window.

Discussion stats
• 5 replies
• 119 views
• 0 likes
• 4 in conversation