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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 1557 views
  • 1 like
  • 3 in conversation