DATA Step, Macro, Functions and more

Simple Program

Reply
Contributor
Posts: 41

Simple Program

I have a dataset similar to:

age frequency
20 2
21 3
23 5
24 3

and I want to create a new dataset from the above dataset that looks like:

age frequency
20 1
20 1
21 1
21 1
21 1
23 1
23 1
23 1
23 1
23 1
24 1
24 1
24 1

How can I accomplish this. Thanks.
SAS Super FREQ
Posts: 8,868

Re: Simple Program

Hi:
Inside a DATA step program, you would need to use a DO loop. If you want the value of the FREQUENCY variable to change to 1, then you'd probably need to save the original value within your DATA step logic. Something like this untested code shown below.

cynthia
[pre]
DATA new;
. . . more code to read data either with INFILE/INPUT or SET statement . . .
. . . may also want KEEP or DROP statement here . . .

** grab original frequency value into new variable name;
orig_frequency = frequency;

** set frequency value to 1;
frequency=1;

** Use original frequency value in do loop for output;
** control output in DO loop -- one observation will be output;
** for every iteration of the DO loop.;
do i = 1 to orig_frequency;
output new;
end;
. . . more code to end program . . .
run;
[/pre]
Contributor
Posts: 41

Re: Simple Program

Thanks Cynthia. I'll give it a shot.
Contributor
Posts: 41

Re: Simple Program

Thanks Cynthia. It worked perfectly Smiley Happy

proc import out=data
datafile= "c:\file.xlsx"
dbms=excel replace;
range="Sheet1$";
getname=yes;
mixed=no;
scantext=yes;
usedate=yes;
scantime=yes;
run;

data data1;
set data;
orig_frequency=frequency;
frequency=1;
do i = 1 to orig_frequency;
output data1;
end;
run;
Ask a Question
Discussion stats
  • 3 replies
  • 184 views
  • 0 likes
  • 2 in conversation