SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cjlin2020
Calcite | Level 5

Hi,

 

My collaborator sent me his Stata codes. I only use SAS so need help to convert STATA codes to SAS codes for a longitudinal data variable:

Following are his codes:

 

forvalues i=1/13{

gen r`i'ins=1 if r`i'higov==1

replace r`i'ins=2 if r`i'higov==1 & r`i'covr==1

replace r`i'ins=2 if r`i'higov==1 & r`i'covs==1

replace r`i'ins=3 if r`i'higov==1 & r`i'hiothp==1

replace r`i'ins=4 if (r`i'covr==1 | r`i'covs==1) & r`i'ins==.

replace r`i'ins=5 if r`i'hiothp==1 & r`i'ins==.

replace r`i'ins=6 if r`i'higov==0 & r`i'covr==0 & r`i'covs==0 & r`i'hiothp==0

capture label define r`i'ins 1"Government Insurance Only" 2"Governemnt and Employer" ///

3"Government and Other" 4"Employer Only" 5"Other Only" 6"No Insurance"

label value r`i'ins r`i'ins

}

 

 

Appreciate your help.

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

ARRAY processing should solve your problem. What is the naming pattern for your variables? Is it something like this?

array rhigovs (13) r1higov r2higov r3higov r4higov r5higov r6higov
                   r7higov r8higov r9higov r10higov r11higov r12higov r13higov;
array rins (13) r1ins......................r13ins;
do i = 1 to dim(rhigovs);
<put your array logic here.>
if rhigovs(i) = 1 then rins(i) = 1;
<etc>
end;

 

 

 

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

Ask your collaborator what this does and we'll try to help.

It looks like just a few tests.

 

cjlin2020
Calcite | Level 5

The Stata codes were to generate a new insurance categorical variable (r{*}ins, i is from 1 to 13) within each study id repeating for 13 years which is a long form of data.

 

Here is what I have figured out:

 

Using the first year (r1ins) as an example:
 
r1ins=.;
If r1higov =1, then r1ins = 1;
ELSE If (r1higov =1 & r1covr=1) or if (r1higov=1 & r1covs=1) then r1ins = 2;
ELSE If (r1higov=1 & r1hiothp=1) then r1ins = 3;
ELSE If (r1covr=1 or r1covs=1) and (r1ins is missing) then r1ins = 4;
ELSE If (r1hiothp=1 & r1ins is missing)  then r1ins = 5;
ELSE If r1higov=0 & r1covr=0 & r1covs=0 & r1hiothp=0 then r1ins = 6;
 

The last part of the Stata codes were to format the riins, which can be converted to following SAS codes: 

 

PROC FORMAT;
VALUE ins_fmt
            1 = "Government Insurance Only"
            2 = "Government Insurance and Employer"
            3 = "Government Insurance and Other"
            4 = "Employer Only"

            5 = "Other only"

            6 = "No Insurance";
RUN;

 

I still need help to write the SAS codes for all 13 years within each study id.

Thanks.

SASKiwi
PROC Star

ARRAY processing should solve your problem. What is the naming pattern for your variables? Is it something like this?

array rhigovs (13) r1higov r2higov r3higov r4higov r5higov r6higov
                   r7higov r8higov r9higov r10higov r11higov r12higov r13higov;
array rins (13) r1ins......................r13ins;
do i = 1 to dim(rhigovs);
<put your array logic here.>
if rhigovs(i) = 1 then rins(i) = 1;
<etc>
end;

 

 

 

cjlin2020
Calcite | Level 5

Thank you for your help.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2119 views
  • 1 like
  • 3 in conversation